簡體   English   中英

為什么綁定到另一個Ember對象無效?

[英]Why does this binding to another Ember object not work?

我建立了一個簡單的Ember綁定示例,我認為它可以正常工作: http : //jsbin.com/aBekITeT/1/edit

為什么控制器的isTrue屬性不與模型同步? 不要尋找任何“把東西放在窗戶上的壞習慣”之類的東西(除非實際上是在破壞它)。 我在這里尋求了解。

我對http://emberjs.com/guides/object-model/bindings/的閱讀使我確信它應該以這種方式工作。 有人可以解釋我可能解釋的錯誤嗎?

除非那是真正的突破

那就是真正打破它的原因。 綁定僅適用於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中,不認為使用綁定綁定到全局對象是一種好的方式-最佳實踐是使用依賴注入來訪問共享對象。 如果發現自己需要使用全局綁定,則可能是您需要重構的信號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM