Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to notify observer when an Object changes?
    primarykey
    data
    text
    <p><strong>Short version: Is there a way to make sure an observer gets notified of a change, no matter what, even if Ember doesn't think a property has changed?</strong></p> <p>In general, the ember observer pattern works for me, but here I'm stuck. I'm not sure if this is a bug or whatnot, or feature, but something about this bothers me.</p> <p>I would be very happy with, in this occassion, some form of <code>myinstance.fireNotificationsNoMatterWhat("cache")</code>, but I can't find something like that in the code. Is there something I'm missing?</p> <p>Sample code below showing the problem.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;emberjs notification test&lt;/title&gt; &lt;!-- using current (at the time) version 0.9.5, from emberjs.com starter kit. --&gt; &lt;script src="jquery-1.6.1.min.js"&gt;&lt;/script&gt; &lt;script src="ember-0.9.5.min.js"&gt;&lt;/script&gt; &lt;script&gt; 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); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;emberjs notification test&lt;/h1&gt; &lt;h2&gt;Please open your console to see the console.log output.&lt;/h2&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>EDIT:</strong> It looks like there are short term fixes to this problem, however after trying out my own "fix", and trying out Roy's suggestion below, I think my core question will not be answerable until this <a href="https://github.com/emberjs/ember.js/issues/466" rel="nofollow">enhancement request</a> gets some love from the busy ember team. I can wait for that if it eventually gives me what the ArrayProxy gives me.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload