在实例化后,是否可以更改Emberjs对象属性的绑定?

时间:2012-03-14 22:59:45

标签: data-binding ember.js

这似乎不起作用 http://jsfiddle.net/8haXN/7/

App.president = Ember.Object.create({
    name: "Barack Obama",
    name2: "George Bush"
});

App.country = Ember.Object.create({
    presidentNameBinding: 'App.president.name'
});

App.country.set('presidentNameBinding', 'App.president.name2');
App.country.presidentName //stil returns 'Barack Obama'

我真正想要做的是将CollectionView的contentBinding从一个ArrayController更改为另一个。这可能还是个坏主意?

2 个答案:

答案 0 :(得分:0)

您必须以这种方式手动绑定:

App.country.bind('presidentName', Em.Binding.from('App.president.name2'));

这是jsfiddle:http://jsfiddle.net/efPGF/

要阅读有关绑定的更多信息,我建议您查看旧的Sproutcore wiki:http://wiki.sproutcore.com/w/page/12412963/Runtime-Bindings。只需记住将SC前缀更改为Ember。

答案 1 :(得分:0)

使用Ember.bind,请参阅http://jsfiddle.net/8haXN/8/

App = Em.Application.create()
App.president = Ember.Object.create({
    name: "Barack Obama",
    name2: "George Bush"
});

App.country = Ember.Object.create({
    // Ending a property with 'Binding' tells Ember to
    // create a binding to the presidentName property.
    presidentNameBinding: 'App.president.name'
});

App.someController = Em.Object.create({
    change: function() {
        Ember.bind(App, 'country.presidentName', 'App.president.name2');
    }
});​