简版:有没有办法确保观察者获得变更通知,不管是哪一种,即使Ember认为属性没有改变?如何在对象更改时通知观察者?
一般来说,这个烬观察者模式适用于我,但在这里我卡住了。我不确定这是一个错误还是什么,或者什么特性,但是关于这个的事情困扰着我。
在这个场合,我会很高兴,某种形式的myinstance.fireNotificationsNoMatterWhat("cache")
,但我在代码中找不到那样的东西。有什么我失踪?
下面的示例代码显示问题。
<!DOCTYPE html>
<html>
<head>
<title>emberjs notification test</title>
<!-- using current (at the time) version 0.9.5, from emberjs.com
starter kit. -->
<script src="jquery-1.6.1.min.js"></script>
<script src="ember-0.9.5.min.js"></script>
<script>
Test = Ember.Application.create({});
// I don't want an arrayproxy this time, and would be happy to find
// out if there is some form of ObjectProxy?
Test.cacheObject = Ember.Object.create({
"cache": {},
"add": function(key, value) {
// In other situations, I don't need
// propertyWillChange/propertyDidChange, but here
// I'm stuck, and I can't notify listeners when my
// cache changes, so I figured I'd try out
// these commands, which don't work, either.
this.propertyWillChange("cache");
var cache = this.get("cache");
cache[key] = value;
this.set("cache", cache);
this.propertyDidChange("cache");
},
});
// Contrived watcher.
Test.watcher = Ember.Object.create({
// One of the many ways I've tried to bind to the
// the object to get the observer to fire.
"cacheBinding": "Test.cacheObject.cache",
"obs": function() {
// Ideally, whenever I call Test.cacheObject.add in different call
// stacks I should be notified.
console.log("cache has changed:");
console.log(this.get("cache"));
}.observes("cache"),
});
setTimeout(function() {
// I get notified on the first add...
Test.cacheObject.add("hello", "world");
}, 500);
setTimeout(function() {
// ...but I will not get notified on the second, or any subsequent addition.
Test.cacheObject.add("and", "universe");
}, 1000);
</script>
</head>
<body>
<h1>emberjs notification test</h1>
<h2>Please open your console to see the console.log output.</h2>
</body>
</html>
编辑:它看起来像有尝试自己的“修复”,而下面尝试罗伊的建议后却是短期的修复这个问题,我想我的核心问题不会回答的,直到这个enhancement request从繁忙的余烬队中获得了一些爱。我可以等待,如果它最终给我什么ArrayProxy给我。
如果您在示例中注意到了,我正在这样做。如果你运行代码,它不起作用,或者说它只能运行一次。我会很高兴地发现我的代码是错误的**,并且现在有一个解决方案,但是我认为,鉴于我在github上看到的有关ember的内容,可能需要等待ObjectProxy 。 – jeremyosborne 2012-02-23 04:09:43
嗨ud3323,我应该澄清,你是对的,我上面的代码没有明确尝试静态'Ember.propertyWillChange'和'Ember.propertyDidChange',而是它使用实例方法。然而,不幸的是,我已经尝试过静态版本,而且它们也不工作。悲伤的脸。 – jeremyosborne 2012-02-23 04:15:45
抱歉,只能阅读短版。没有看到一个jsFiddle来运行代码,但我会为你做一个。请给我几分钟时间查看您的代码。 – 2012-02-23 04:17:40