这很可能是最好使用嵌套视图解决。你可以看到my jsFiddle here。
我不认为这是一个非常复杂的方法,但如果您有任何关于设计的问题,请让我知道。
把手:
<script type="text/x-handlebars">
{{#each App.countriesController}}
{{view Ember.Checkbox titleBinding="name" valueBinding="isChecked"}}
{{/each}}
<hr />
{{#each App.countriesController}}
{{#if isChecked}}
{{view App.PartsByCountryView contentBinding="this" partsListBinding="App.partsController"}} <br />
{{/if}}
{{/each}}
</script>
<script type="text/x-handlebars" data-template-name="selected-country">
Country: {{content.name}} <br />
Parts:
{{#each filteredParts}}
{{name}}
{{/each}}
</script>
应用代码:
window.App = App = Ember.Application.create();
/**************************
* Models
**************************/
App.Country = Ember.Object.extend({
id: null,
name: "",
isChecked: null
});
App.Part = Ember.Object.extend({
id: null,
name: "",
countryId: null
});
/**************************
* Views
**************************/
App.PartsByCountryView = Ember.View.extend({
content: null,
partsList: null,
templateName: 'selected-country',
filteredParts: function() {
return this.get("partsList").filterProperty('countryId', this.getPath('content.id'))
}.property('[email protected]').cacheable()
});
/**************************
* Controllers
**************************/
App.set('countriesController', Ember.ArrayController.create({
content: Ember.A()
}));
App.set('partsController', Ember.ArrayController.create({
content: Ember.A()
}));
/**************************
* App Logic
**************************/
$(function() {
App.get('countriesController').pushObjects([
App.Country.create({id: 1, name: "USA"}),
App.Country.create({id: 2, name: "UK"}),
App.Country.create({id: 3, name: "FR"})
]);
App.get('partsController').pushObjects([
App.Part.create({id: 1, name: "part1", countryId: 1}),
App.Part.create({id: 2, name: "part2", countryId: 1}),
App.Part.create({id: 3, name: "part3", countryId: 1}),
App.Part.create({id: 4, name: "part4", countryId: 2}),
App.Part.create({id: 5, name: "part5", countryId: 2}),
App.Part.create({id: 6, name: "part6", countryId: 2}),
App.Part.create({id: 7, name: "part7", countryId: 2}),
App.Part.create({id: 8, name: "part8", countryId: 3}),
App.Part.create({id: 9, name: "part9", countryId: 3}),
App.Part.create({id: 10, name: "part10", countryId: 3}),
App.Part.create({id: 11, name: "part11", countryId: 3})
]);
});
嗨罗伊谢谢你的伟大答案。只有一个问题:如果我转到控制台并尝试添加另一部分像这样'App.get('partsController')。pushObjects(App.Part.create({id:999,name:“part999”,countryId: 2})]); '我如何将它添加到相关部分而不必点击复选框并再次打开?再次感谢 – 2012-03-06 13:50:04
对不起,我忘了添加一个属性来观察我的filteredParts计算属性的定义。我更新了代码和jsFiddle。这个成功地回答了你的问题吗? – 2012-03-06 13:57:38
哇,非常快,非常感谢你,是的,这已经完全解答了我的问题。 – 2012-03-06 14:02:53