问题
I am iterating a list of entries and would like to display the content of each entry within the list, but only when on the respective entry's route.
i.e. - When on route /entries/2
* link to entry 1
* link to entry 2
content for entry 2
* link to entry 3
Unfortunately, it seems I can't use {{outlet}} within the {{#each entry}} loop.
Initially I tried to set isActive to true within setupController on the route, and check for that within the template, but it does not seem like there is a good way to remove that flag when you navigate to /entries/3 (deactivate only works when moving away from /entries/:entry_id entirely) . See Is there an opposite of setupController? for more on this.
What is the best way to do this with Ember?
回答1:
You can use a an itemController with {{each}} and set a computed property on that item controller that makes sure the current model property is equal to the App.EntryRoute current model.
So you would have the following route setup:
App.Router.map(function() {
this.resource('entries', { path: '/'}, function() {
this.resource('entry', { path: ':entry_id' });
});
});
The following template:
<script type="text/x-handlebars" id="entries">
{{#each controller itemController="entryItem"}}
{{#linkTo "entry" this}}{{name}}{{/linkTo}} <br />
{{#if isSelected}}
{{details}} <br /><br />
{{/if}}
{{/each}}
No what you need to do is create the App.EntryItem controller and add a computed property isSelected which should return true if current route model === model
something like this:
App.EntryItemController = Em.ObjectController.extend({
needs: 'entry',
isSelected: function() {
return this.get('controllers.entry.model') === this.get('model');
}.property('controllers.entry.model')
});
Here's a fiddle with all of the above applied:
http://jsfiddle.net/teddyzeenny/T2EyK/1/
回答2:
I achieved the solution in the following way. Please let me know any short comings in my code. I am setting a property to Entry model, when calling the entry route to make it visible.
HTML:
<script type="text/x-handlebars" >
{{outlet}}
</script>
<script type="text/x-handlebars" id="entries">
{{#each controller }}
{{#linkTo "entry" this}}{{name}}{{/linkTo}} <br />
{{#if visi}}
<p>{{details}}</p>
{{/if}}
{{/each}}
</script>
JAVASCRIPT:
App = Ember.Application.create();
App.Router.map(function(){
this.resource('entries', { path: '/'}, function() {
this.resource('entry', { path: 'entry/:entry_id' });
});
});
App.Entry = Em.Object.extend({
id: null,
name: null,
details: null
});
App.entries =[
App.Entry.create({
id: 1,
name: 'entry 1',
details: 'details 1'
}),
App.Entry.create({
id: 2,
name: 'entry 2',
details: 'details 2'
}),
App.Entry.create({
id: 3,
name: 'entry 3',
details: 'details 3'
})
];
App.EntriesRoute=Ember.Route.extend({
model:function() {
return App.entries;
}
});
App.EntryRoute=Ember.Route.extend({
model:function(params) {
var id=parseInt(params.entry_id);
return App.entries.findProperty('id',id);
},
setupController:function(controller,model){
App.entries.setEach('visi',false);
model.set('visi',true);
}
});
Regards.
来源:https://stackoverflow.com/questions/16508652/how-can-i-render-a-models-template-in-a-list-only-when-on-the-models-route