我在Ember有一个这样的设置:
App.ListObject = Ember.Object.create({
knownThings: function() {
var ot = this.openThings.get('content');
var ct = this.closedThings.get('content');
var kt = ot.concat(ct);
var known = Ember.ArrayController.create({content: kt});
return known;
}.property(),
openThings: Ember.ArrayController.create({
content: []
}),
closedThings: Ember.ArrayController.create({
content: []
}),
})
基本上,已知的东西是openThings和closedThings的组合数组。我似乎无法弄清楚如何在模板中迭代knownThings。只是做
openThings: Ember.ArrayController.create({
content: []
}),
closedThings: Ember.ArrayController.create({
content: []
}),
})
不起作用,因为需要像
{{#each App.ListObject.knownThings }}
那样访问该属性,但这在模板中不起作用,除非我做的事情非常糟糕。迭代模板中的其他属性可以正常工作(打开和关闭的东西)
那么,你将如何在模板中迭代knownThings?
答案 0 :(得分:3)
需要轻微修改......
首先,
knownThings: function() {
//use get to retrieve properties in ember, Always !
var ot = this.get('openThings').get('content');
//var ot = this.get('openThings.content') if you are using latest ember
var ct = this.get('closedThings').get('content');
//var ot = this.get('closedThings.content') if you are using latest ember
var kt = ot.concat(ct);
var known = Ember.ArrayController.create({content: kt});
return known;
//Add dependencies to keep your knownThings in sync with openThings & closedThings if at all they change in future
}.property('openThings', 'closedThings')
使用
来使用Handlebars迭代//you forgot content property, and in handlebars you don;t need to use get, dot operator is enough
{{#each App.List.knownThings}}
让我知道这是否有效......
更新的 工作Fiddle ...
答案 1 :(得分:1)
除非我不明白你在说什么,否则我认为你应该ListObject
延长Em.ArrayController
而不是Em.Object
。另外,如果您的财产取决于content
,则应为.property('content.@each')
。如果您使用的是路由器,那么您的模板应该看起来像{{#each thing in controller.knownThings}}
,如果不使用路由器则使用{{thin.something}}
,然后使用{{#each item in App.listObject.knownThings}}
。此外,openThings
和closedThings
似乎不正确,您访问它们的方式也是错误的。
我没有为这个具体案例写一个小提琴因为我真的不知道你要做什么,但看看this fiddle,特别是App.ResourcesController
和模板'资源视点':
控制器:
// ...
App.ResourcesController = Em.ArrayController.extend({
content: [],
categories: ['All', 'Handlebars', 'Ember', 'Ember Data', 'Bootstrap', 'Other'],
categorySelected: 'All',
filtered: function() {
if(this.get('categorySelected') == "All") {
return this.get('content');
} else {
return this.get("content")
.filterProperty(
"category",
this.get('categorySelected')
);
}
}.property('content.@each', 'categorySelected'),
filteredCount: function() {
return this.get('filtered').length;
}.property('content.@each', 'categorySelected'),
hasItems: function() {
return this.get('filtered').length > 0;
}.property('filteredCount')
);
// ...
模板:
<script type="text/x-handlebars" data-template-name="resources-view">
<h1>Ember Resources</h1>
{{#view Bootstrap.Well}}
The following is a list of links to Articles, Blogs, Examples and other types of resources about Ember.js and its eco-system.
{{/view }}
{{view Bootstrap.Pills contentBinding="controller.controllers.resourcesController.categories" selectionBinding="controller.controllers.resourcesController.categorySelected"}}
<i>{{controller.filteredCount}} Item(s) Found</i>
{{#if controller.hasItems}}
<ul>
{{#each resource in controller.filtered}}
<li>
<a {{bindAttr href="resource.url"
target="resource.target"
title="resource.description"}}>
{{resource.htmlText}}
</a>
</li>
{{/each}}
</ul>
{{else}}
{{#view Bootstrap.AlertMessage type="warning"}}
Couldn't find items for {{controller.categorySelected}}
{{/view}}
{{/if}}
</script>