【问题标题】:Ember.js ArrayController hooked into CollectionView: How to update object properties in 'content' array?Ember.js ArrayController 连接到 CollectionView:如何更新“内容”数组中的对象属性?
【发布时间】: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


【解决方案1】:

尝试App.Entities.objectAt(0).set('name', "New name") 并告诉我这是否可行...数组必须包含 Ember 对象才能使绑定适用于所做的更改

【讨论】:

  • 感谢您的回复.. 不幸的是:TypeError: Object #<Object> has no method 'set'
  • 想知道它是否与 _childViews 有关?
  • 哟!我得到了答案,非常感谢你引导我做正确的事情!所以为了让它工作,你必须确保数据数组中的对象是 Ember 对象.. 然后你可以使用 @each.name 上的绑定来确保它们被拾取计算属性!
  • 哦,是的,我忘了提到您需要使用 Ember 对象进行设置和获取,显然是为了使绑定起作用,更新了答案...很高兴知道它起作用了 :)
  • 请阅读整个评论线程以获得答案。 TL;DR:您需要观察 Ember.Objects 上的属性才能进行绑定。它不适用于普通对象。伙计,我怎么记得从 Ember 开始的挑战:)
猜你喜欢
  • 2012-06-10
  • 1970-01-01
  • 2012-09-14
  • 2012-05-13
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多