在下面的 jsFiddle 中,我添加了一个小示例 ember 应用程序。在“详细信息”视图中,我想显示有关所选组的信息,如果我单击组中的设备,我想用设备详细信息替换详细信息。我如何以 ember 方式管理这个?
http://jsfiddle.net/theremin/qGCe6/1/
模板
<script type="text/x-handlebars" data-template-name="application">
<h2>SampleApp</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="groups">
<div>
<h3>Groups</h3>
<div style="">
<ul>
{{#each controller}}
<li>{{#linkTo "devices" devices}}{{name}}{{/linkTo}}</li>
{{/each}}
</ul>
</div>
</div>
<div>
{{render 'devices'}}
</div>
<div>
{{render 'details'}}
</div>
</script>
<script type="text/x-handlebars" data-template-name="devices">
<h3>Devices</h3>
<div>
<ul>
{{#each model}}
<li>{{#linkTo "details" App.Device}}{{name}}{{/linkTo}}</li>
{{/each}}
</ul>
</div>
</script>
<script type="text/x-handlebars" data-template-name="details">
<h3>Details for:</h3>
<div>
<i>{{model}}</i>
</div>
</script>
JAVASCRIPT
App = Ember.Application.create({});
App.Store = DS.Store.extend({
revision : 12,
adapter : 'DS.FixtureAdapter'
});
App.Router.map( function() {
this.resource('groups', function() {
this.resource('devices', { path: "/:group_id" });
this.resource('details', { path: "/:device_id" });
});
});
/**
* ROUTES
*/
App.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('groups');
}
});
App.GroupsRoute = Ember.Route.extend({
model : function( ){
//console.log("--->", App.Group.find());
return App.Group.find();
}
});
/*App.DetailsRoute = Ember.Route.extend({
model : function(){
return this.modelFor("details");
}
});*/
/**
* MODELS
*/
App.Group = DS.Model.extend({
name: DS.attr('string'),
details: DS.attr('string'),
devices: DS.hasMany('App.Device')
});
App.Device = DS.Model.extend({
name: DS.attr('string'),
details: DS.attr('string'),
groups: DS.hasMany('App.Group')
});
/**
* FIXTURES
*/
App.Group.FIXTURES = [{
id:1,
name:"Group one",
details:"details for Group one",
devices:[1,2]
},{
id:2,
name:"Group two",
details:"details for Group two",
devices:[2]
}];
App.Device.FIXTURES = [{
id:1,
name:"Device1",
details:"details for Device1",
groups: [1]
},{
id:2,
name:"Device2",
details:"details for Device2",
groups:[2]
}];