2012-03-14 94 views
1

這似乎並不工作 http://jsfiddle.net/8haXN/7/是否可以在實例化後更改Emberjs對象屬性的綁定?

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' 

我真正想要做的是從一個ArrayController改變的CollectionView的contentBinding到另一個。這是可能的還是這是一個壞主意?

回答

0

我們將看到這樣的方式來手動綁定:

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

這裏的的jsfiddle:http://jsfiddle.net/efPGF/

想了解更多關於綁定,我建議在看老SproutCore的維基:http://wiki.sproutcore.com/w/page/12412963/Runtime-Bindings。請記住將SC前綴更改爲Ember。

+0

感謝您的回答,鏈接的jsfiddle沒那麼我更新了它的工作:http://jsfiddle.net/efPGF/1/ – 2012-03-16 14:58:32

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'); 
    } 
});​ 
+0

謝謝您的回答!很高興知道有多種方法來設置/更改綁定! – 2012-03-19 17:34:51

+0

儘管在我現有的Ember.js應用程序中,沒有必要按照您的要求手動更改綁定。我幾乎總是隻是更改控制器的實際內容,而不是創建新的綁定。您是否要求改變綁定的一般方法,或者您是否有要解決的具體問題? – pangratz 2012-03-19 18:10:45

相關問題