javascript - 为什么与另一个 Ember 对象的绑定(bind)不起作用?

标签 javascript ember.js

我已经设置了一个简单的 Ember 绑定(bind)示例,我相信应该可以工作: http://jsbin.com/aBekITeT/1/edit

为什么 Controller 的 isTrue 属性与模型的不同步?不要寻找任何“把东西放在 window 上是不好的做法”之类的东西(除非这实际上是破坏它的原因);我在这里寻求理解。

我读过的http://emberjs.com/guides/object-model/bindings/说服我它应该这样工作。有人可以解释一下我可能解释错误的内容吗?

最佳答案

unless that's actually what's breaking it

这才是真正破坏它的原因。绑定(bind)仅适用于 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.                    +---------------+

当绑定(bind)以大写字母开头时,指的是全局对象,例如:

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

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

但是,在 Ember 中,使用绑定(bind)来绑定(bind)全局对象并不被认为是好的风格 - 最佳实践是使用依赖注入(inject)来访问共享对象。如果您发现自己需要使用全局绑定(bind),这可能是您需要重构的标志。

关于javascript - 为什么与另一个 Ember 对象的绑定(bind)不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20978904/

相关文章:

javascript - react 中的排序表。只有一列有效时怎么可能?

javascript - 使用返回的 promise 数据

javascript - 如何从 Ember JS 的子路由访问父 Controller ?

javascript - Ember.js 路由 transitionTo 方法不起作用

javascript - 为什么我的图表在 Chrome 中是错误的?

javascript - 如何创建一个 Ember JS Master Detail,其中细节出现在数据行之间

javascript - 如何获取屏幕上html标签的位置?

javascript - Istanbul 尔纽约将包含测试文件

javascript - 动态 js 文件的标准解决方法是什么

javascript - 关键字和文字之间需要空格