Call specific Component in Ember - ember.js

I've got a Component "comments"
<div class="comments">
<ul>
{{#each comment in comments}}
...
</ul>
</div>
which is present multiple times in the HTML:
Product A:
{{comments}}
Product B:
{{comments}}
etc.
If I set "comments" all Components render the comments. How can I tell Ember to set the Comments for a specific Product only? e.g. on headline click and Ajax Load.
JS Bin: http://jsbin.com/wafacojenahe/2/edit

Two issues with your code.
You didn't have any hierarchy (or sets of) in the data. This has a basic structure in place:
App = Ember.Application.create();
App.IndexRoute = Ember.Route.extend({
model: function() {
return [
{
'name': 'Product A',
'comments': [
'Lorem ipsum',
'dolor sit amet'
]
},
{
'name': 'Product B',
'comments': [
"Another comment"
]
}
];
}
});
And the other issue is that components always need to be passed all data in.
<script type="text/x-handlebars" data-template-name="index">
{{#each product in model}}
<h1>{{product.name}}</h1>
{{comments-list model=product.comments}}
{{/each}}
</script>
<script type="text/x-handlebars" id="components/comments-list">
<ul>
{{#each model}}
<li>{{this}}</li>
{{/each}}
</ul>
</script>
JS Bin: http://jsbin.com/wafacojenahe/3/

Related

Ember - partial template with fixture data

I just started with Ember and have a basic web app structure in place. I have a partial template set up and want to bring some test data into it. I can see my test data if I use my 'home' template for it, but I can not get it into my collections partial template. I'm almost certain it has to be related to basic Ember concepts that I have no familiarity with. Here is my simplified html and below is the Ember js. If anybody has any idea how I could bring the 'each' loop from 'home' template into my partial 'collections' template, I'll be very thankful:
[code]
<script type="text/x-handlebars" ><!-- data-template-name="application" -->
<div id="main">
<ul>
<li>
<image src="logo.png" style="width:439px;height:102px;"/>
</li>
<li>{{#link-to 'home'}}Home{{/link-to}} | {{#link-to 'help'}}Help{{/link-to}}</li>
</ul>
</div>
<div class="collections">
{{partial 'collections'}}
</div>
<div class="statistics">
{{outlet}}
</div>
</script>
<script type="text/x-handlebars" id="collections">
COLLECTIONS<br/>
<!-- here is where I wand the 'each' loop to go -->
</script>
<script type="text/x-handlebars" id="home">
OnLoad : This is Home Page<br/>
<ul>
{{#each}}
<li>
<label>{{title}}</label>
</li>
{{/each}}
</ul>
</script>
<script type="text/x-handlebars" id="help">
Welcome to Help Page
</script>
[/code]
And here is JS code for the above:
[code]
App = Ember.Application.create();
App.Router.map(function(){
this.resource('home', { path: '/' });
this.resource('help');
});
App.CollectionsRoute = Ember.Route.extend({
model: function(){
return items;
}
});
App.HomeRoute = Ember.Route.extend({
model: function(){
return items;
}
});
var items = [
{
id: 1,
title: 'Item 1',
belongsTo: 'parent path 1'
},{
id: 2,
title: 'Item 2',
belongsTo: 'parent path 2'
},{
id: 3,
title: 'Item 3',
belongsTo: 'parent path 3'
}
];
[/code]
partial 'collections' have the same context of application template. So if you declare:
App.ApplicationRoute = Ember.Route.extend({
model: function(){
return items;
}
});
Will be able to use:
<script type="text/x-handlebars" id="collections">
COLLECTIONS<br/>
<ul>
{{#each}}
<li>
<label>{{title}}</label>
</li>
{{/each}}
</ul>
</script>

Ember template not showing any data

I am just starting out with Ember.js and I am using ember-rails on the back end. The problem that I am having is that I am getting a list of items but no data is being rendered in my template.
router.js.coffee
App.Router.map (match)->
# match('/').to('index')
#resource "stories"
stories_route.js.coffee
App.StoriesRoute = Ember.Route.extend
model: ->
App.Story.find()
stories.handlebars
<h1>Stories</h1>
<ul>
{{#each story in controller}}
<li>{{story.title}} A</li>
{{else}}
<li>There are no stories.</li>
{{/each}}
</ul>
{{outlet}}
Here is the JSON that I am getting back from Rails:
{
"stories":[
{
"story":{
"id":1,
"title":"Test",
"description":"This is a test story"
}
}
]
}
Edit:
I am able to get the correct template to render, it is just that the data is empty.
Here is what the HTML looks like:
<div id="ember295" class="ember-view">
<h1>Stories</h1>
<ul>
<script id="metamorph-2-start" type="text/x-placeholder">
</script>
<script id="metamorph-4-start" type="text/x-placeholder">
</script>
<li>
<script id="metamorph-5-start" type="text/x-placeholder">
</script>
<script id="metamorph-5-end" type="text/x-placeholder">
</script>
A
</li>
<script id="metamorph-4-end" type="text/x-placeholder">
</script>
<script id="metamorph-2-end" type="text/x-placeholder">
</script>
</ul>
<script id="metamorph-3-start" type="text/x-placeholder">
</script>
<script id="metamorph-3-end" type="text/x-placeholder"></script>
</div>
If stories is your first route and accessible at "/" then you can define a path like:
App.Router.map () ->
#resource('stories', {path: '/'})
OR you can define an index route and redirect from there like:
App.IndexRoute = Ember.Route.extend
redirect: ->
#transitionTo('stories')
See here a working jsbin that shows the second concept.
EDIT
I guess your problem is now that if your result contains a collection of records you need to remove the additional story level in your JSON resulting in something like:
{
"stories":[
{
"id":1,
"title":"Test",
"description":"This is a test story"
}
]
}
and for a single record e.g. /stories/123 your JSON should look like:
{
"id":1,
"title":"Test",
"description":"This is a test story"
}
Hope it helps

How do I pass model information to this each statement in a nested template?

I have an index route where I need to populate a menu in one outlet based on a list of models. However I cannot get the models to be represented in the each statement.
Here is the Category Model:
App.Category = DS.Model.extend({
title: DS.attr('string'),
parent: DS.belongsTo('App.Category')
})
And here is the indexRoute where I render it
App.IndexRoute = Em.Route.extend({
renderTemplate: function(){
this.render('index')
this.render('categoryMenu',
{
outlet: 'sidebar',
into: 'index',
model: function() {
return App.Category.find()
},
controller: App.CategoriesController
})
this.render('badgesList',{
outlet: 'badgesList',
into: 'index'
})
}
})
Index Template:
<script type="text/x-handlebars" data-template-name="index">
<div class="span3">
{{outlet sidebar}}
</div>
<div class="span8">
{{outlet badgesList}}
</div>
</script>
Nested Category Template
<script type="text/x-handlebars" data-template-name="categoryMenu">
<ul>
{{#each model}}
{{title}}
{{/each}}
</ul>
</script>
I have tried to change the each statement to several different things like controller or item in model but nothing is displayed.
Thank you for any help!
Make yourself CategoriesController first like this:
App.CategoriesController = Ember.ArrayController.extend({
content: function () {
return App.Category.find()
}.property()
});
rename your template just to categories
<script type="text/x-handlebars" data-template-name="categories">
<ul>
{{#each model}}
{{title}}
{{/each}}
</ul>
</script>
in your index template replace {{outlet}} with {{render 'categoies'}}
<script type="text/x-handlebars" data-template-name="index">
<div class="span3">
{{render 'categoies'}}
</div>
<div class="span8">
{{outlet badgesList}}
</div>
</script>
as last thing remove the call rendering categoryMenu.

Multiple views with Ember

I am researching Ember and i wanna know if it is possible to include multiple views in a single page and switch between layout templates. I've developed with AngularJS before.
I am searching for an Ember equivalent of this $route.when('/view1', ...); and <ng-include src="templates.top" />.
Here is a working fiddle for Angular. I hope someone can help me because i found little help.
In it's simplest form, you can use StateManager. Please see the following fiddle I created: http://jsfiddle.net/npCfF/
Javascript:
App= Ember.Application.create();
App.StateManager = Ember.StateManager.create({
rootElement: '.tab-content',
initialState: 'tab1',
//Show the location tab function
showTab1: function(manager) {
manager.transitionTo('tab1');
},
//show seleceted areas
showTab2: function(manager) {
manager.transitionTo('tab2');
},
showTab3: function(manager) {
this.set('locationActive', 'inactive');
this.set('areasActive', 'active');
this.set('filterActive', 'inactive');
this.set('childOf', 'showAreas');
manager.transitionTo('tab3');
},
tab1: Ember.ViewState.create({
route: 'tab1',
view: Ember.View.create({ templateName: 'tab1' })
}),
tab2: Ember.ViewState.create({
route: 'tab2',
view: Ember.View.create({ templateName: 'tab2' })
}),
tab3: Ember.ViewState.create({
route: 'tab3',
view: Ember.View.create({ templateName: 'tab3' })
})
});
​
HTML:
<script type="text/x-handlebars">
<nav class="tab_menu">
<span id="tab_location_result" data-show="location_result" {{action "showTab1" target="App.StateManager"}}><i class="icon-globe"></i>Tab 1</span> |
<span id="tab_selected_areas" data-show="selected_areas" {{bindAttr class="MapSearch.StateManager.areasActive"}} {{action "showTab2" target="App.StateManager"}}><i class="icon-map-marker"></i>Tab 2</span> |
<span id="tab_filter_results" data-show="filter_results" {{bindAttr class="MapSearch.StateManager.filterActive"}} {{action "showTab3" target="App.StateManager"}}><i class="icon-filter"></i>Tab 3</span>
</nav>
</script>
<div class="tab-content"></div>
<script type="text/x-handlebars" data-template-name="tab1">
Tab1
</script>
<script type="text/x-handlebars" data-template-name="tab2">
Tab2
</script>
<script type="text/x-handlebars" data-template-name="tab3">
Tab3
</script>
​
What you're thinking of is the Ember.js router, with Handlebars.js templates on top. Here is a good guide to it, it covers everything you'll need to get started. Reading through and understanding the tutorial might take some time.

ember.js statemanager use for tabs

i have 3 tabs navigation and the content of each tab is different obviously.
i'm thinking about using StateManager in Emberjs to manager my tab views.
http://docs.emberjs.com/#doc=Ember.StateManager&src=false
is that a good idea? or is there a better router out there? i've looked at
sproutcore-routing
ember-routemanager
are those better than the statemanager? what's the reason not to use statemanager?
You can use something along this lines, see http://jsfiddle.net/pangratz666/e3wM7/:
Handlebars:
<script type="text/x-handlebars" >
<ul>
<li {{action "showFirst" target="App.stateManager"}} >First tab</li>
<li {{action "showSecond" target="App.stateManager"}} >Second tab</li>
</ul>
<div class="tab-content" ></div>
</script>
<script type="text/x-handlebars" data-template-name="first" >
first
</script>
<script type="text/x-handlebars" data-template-name="second" >
second
</script>​
JavaScript:
App.stateManager = Ember.StateManager.create({
rootElement: '.tab-content',
initialState: 'firstTab',
showFirst: function(manager) {
manager.goToState('firstTab');
},
showSecond: function(manager) {
manager.goToState('secondTab');
},
firstTab: Ember.ViewState.create({
view: Ember.View.extend({ templateName: 'first' })
}),
secondTab: Ember.ViewState.create({
view: Ember.View.extend({ templateName: 'second' })
})
});​
Also take a look at the blog post Anatomy of an Ember.js App Part I Redux: Routing and Outlets.