我已经设置了一个简单的 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/