Is Observe.js gone in 1.0

89 views
Skip to first unread message

Joel Weber

unread,
Jun 3, 2015, 10:27:45 AM6/3/15
to polym...@googlegroups.com
I see in the migration notes that Array observation has been replaced by methods (push, pop, etc...) does that mean that an Array needs to be accessible via 'this' to be observed now or is there still something under the hood you can hook into?

Joel Weber

unread,
Jun 3, 2015, 12:03:08 PM6/3/15
to polym...@googlegroups.com
Nevermind, looked through the source and yep, Array's have to be on the path. So to get around that I made a lightweight ObservedArray class, you can grab it here if it's useful to anyone else who, like me, made pretty heavy use of Array.observe. https://github.com/hobberwickey/observables

basic usage: 

var arr = new ObservedArray(),
    observer
= arr.observe( function(splices){ console.log('cool right?', splices) } );


arr
.push("observed!"); //will output "'cool right', [{index: 0, removed: [], addedCount: 1}]"
observer
.close(); //removes the observer

You can also batch calls so you don't have to call the observers after every push, pop, etc...

var arr = new ObservedArray(),
      batch
= arr.batch();


batch
.push("no observers run");
batch
.push("until you flush");
batch
.flush();
Reply all
Reply to author
Forward
0 new messages