How to notify observer when an Object changes?
http://stackoverflow.com/questions/9405078/how-to-notify-observer-when-an-object-changes/9406901#9406901
HTML
<script src="https://github.com/downloads/emberjs/ember.js/ember-latest.js"></script>
<script src="https://github.com/downloads/emberjs/data/ember-data-latest.js"></script>
<script type="text/x-handlebars">
</script>
JavaScript
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: Ember.Object.create({ data:{} }),
// Your cache is now an Ember.Object with key `data` that will hold your data.
add: function(key, value) {
var cache = this.get('cache');
cache.get('data')[key] = value;
// Here you notify your app that your cache's data changed
cache.notifyPropertyChange('data');
}
});
// Contrived watcher.
Test.watcher = Ember.Object.create({
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.data"),
});
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);