2014-01-07 74 views
0

我已經設置了灰燼的一個簡單的例子,結合我相信應該工作:http://jsbin.com/aBekITeT/1/edit爲什麼綁定到另一個Ember對象不起作用?

爲什麼控制器的IsTrue運算性能無法與模型的同步?沒有尋找任何「把事情放在窗口上」這種做法是不好的做法(除非這實際上是打破它的);我在這裏尋找理解。

我的閱讀http://emberjs.com/guides/object-model/bindings/說服我應該這樣工作。有人可以解釋我可能會解釋錯誤嗎?

回答

3

,除非這實際上是怎麼打破它

那什麼是真正打破它。綁定僅適用於Ember對象。 window不是Ember對象。還有的路徑是錯誤的問題,這裏有一個圖:

if isTrueBinding is "appController.isTrue",  This will: 
    this won't work 
       +--------------+     +--------------+ 
       |    |     |    | 
     +-------+| window |+-----+    | window | 
     |  |    |  |    |    | 
     |  +--------------+  |    +--------------+ 
     v        v     + 
    +---------------+  +--------------+   | 
    | appController |  | mod   |   | 
    |---------------|  |--------------|   v 
    | isTrue  |  |isTrueBinding |  +--------------+ 
    |    |  |    |  | mod   | 
    |    |  |    |  |--------------| 
    |    |  |    |  |isTrueBinding | 
    |    |  |    |  |appController |+---> +---------------+ 
    |    |  |    |  |    |  | appController | 
    |    |  |    |  |    |  |---------------| 
    |    |  |    |  |    |  | isTrue  | 
    +---------------+  +--------------+  |    |  |    | 
                |    |  |    | 
                |    |  |    | 
                +--------------+  |    | 
                     |    | 
                     |    | 
      i.e. bindings are implicitly pointing to "this",   |    | 
      unless they start with a capital letter.     +---------------+ 

當綁定做一個大寫字母開頭,則是指一個全局對象,如:

window.Currency = Em.Object.create({ 
    "USD": "$" 
}); 

App.MyObject = Em.Object.extend({ 
    currencyBinding: "Currency.USD" 
}); 

然而,使用綁定綁定到全局對象在Ember中被認爲不是很好的樣式 - 最好的做法是使用依賴注入來訪問共享對象。如果你發現自己需要使用全局綁定,這可能是你需要重構的一個標誌。

+0

但appController是一個燼對象?我沒有約束窗口 – FellowMD

+2

的屬性,實際上這甚至不是唯一被破壞的東西。綁定是相對於你聲明它們的對象。所以你綁定到'appController.isTrue'指向window.mod.appController.isTrue,而你想要的屬性是在window.appController.isTrue。即使你可以通過窗口對象綁定(你不能),它不會工作,因爲路徑是錯誤的 – alexspeller

+0

http://emberjs.com/guides/object-model/bindings/似乎暗示它是一個全球性的路徑 – FellowMD

相關問題