【发布时间】:2012-12-07 17:47:17
【问题描述】:
ember newjack 在这里。将 ArrayController 挂接到 CollectionView 中。
// the ArrayController
App.Entities = Ember.ArrayController.create({
content: Ember.A([
{id: 1, name: "item 1"},
{id: 2, name: "item 2"},
...
{id: n, name: "item n"}
])
});
// the CollectionView
myView = Ember.CollectionView.create(
contentBinding: "App.Entities",
tagName: 'ul',
itemViewClass: Ember.View.extend({
template: Ember.Handlebars.compile('{{view.content.name}}')
})
).appendTo("mySelector");
正如预期的那样,用我的属性创建了一个不错的ul。
同样,所有数组级别的操作(例如弹出、推送和反转)都非常出色:
App.Entities.reverseObjects(); // Works!
App.Entities.popObject(); // Works!
但是,我似乎无法更新数组内部的属性:
App.Entities.objectAtContent(0).name = "new name" // I know this is wrong
有趣的是,如果我之后执行数组操作,它会起作用:
App.Entities.reverseObjects(); // change is picked up!
所以问题是:如何更新 ArrayController 内部的属性(并确保更新绑定?)
顺便说一句,我已经尝试了所有我能想到的......例如 myView.rerender() 等,但我知道我只是做错了,因为它违背了事情的方式应该 工作。
【问题讨论】:
-
做更多的挖掘工作,我注意到我可以替换数组中的一个对象:
App.Entities.insertAt(2,{name: "new name", id=99});或App.Entities.replaceContent(2, 1,[{name: "new name", id=99}]);但是.. 只是改变感觉有点矫枉过正一个属性..
标签: controller ember.js collectionview