2012-03-02 24 views
1

當從一個對象創建一個視圖時,似乎當我改變對象中的一個屬性時,視圖的屬性也會改變。如果我在視圖中更改屬性,則更改不會反映在對象中。我認爲雙向綁定是默認行爲。我錯過了什麼嗎?你如何做雙向綁定使用變量?

WidgetClass = Ember.Object.extend 
    address: 'widget address' 
    create_view: -> 
    # console.log this.name 
    view = Ember.View.create 
     someobj: this 
     addressBinding: 'someobj.address' 
     template: Ember.Handlebars.compile '{{address}}' 

    return view 

TextWidget = WidgetClass.create() 

view = TextWidget.create_view() 

view.append() 

view.set 'address', 'new new address' 
console.log (view.get 'address') 
console.log (TextWidget.get 'address') # I am expecting this output to be 'new new address' 

http://jsfiddle.net/rkitamura/2zsUX/

回答

2

在應用綁定,然後立即測試他們,你需要強制灰燼運行循環追趕。嘗試在view.set 'address', 'new new address'之前添加Ember.run.sync()。應該不再需要您的setTimeout()調用。

+0

謝謝。我試着添加Ember.run.sync(),並且綁定按預期工作。 – Rocky 2012-03-02 19:39:59

+0

很高興提供幫助。如果你全部設置好了,請將問題標記爲已回答。 – 2012-03-02 21:13:20

1

在這種情況下使用setPath很重要。雙向綁定完美​​地起作用。

以下代碼有效。

view.setPath 'someobj.address', 'new new address' 
console.log (TextWidget.get 'address') # this outputs 'new new address' correctly