observed icon indicating copy to clipboard operation
observed copied to clipboard

Automatically observe newly-added objects

Open kurtmilam opened this issue 10 years ago • 3 comments

I'd like to be able to tell an existing observer to immediately and automatically start observing new objects that are added to the observed object.

For instance:

var obj = {}
var observer = O(obj)

observer.on('add a.cool.thing.to', console.log)

obj.a = {cool:{thing:{to:'observe'}}}
// add event for 'to' is logged to console

obj.a = {}
obj.a.cool = {}
obj.a.cool.thing = {}
obj.a.cool.thing.to = 'observe'
// add event for 'to' is logged to console

Could be controlled by a flag and turned off by default. If you're interested, I'll try to work up a pull request.

kurtmilam avatar Jul 23 '14 23:07 kurtmilam

I see you're already automatically observing newly-added objects, so I took a little time to understand why the above code doesn't work like I was hoping it would.

Unless I'm mistaken, there's currently no way to get my add a.cool.thing.to event to fire when adding an object literal as I did in the first example above, i.e. obj.a = {cool:{thing:{to:'observe'}}}.

I can get the event to fire for the second example if I call observer.deliverChanges() just before I set obj.a.cool.thing.to = 'observe'. Here it appears to be a race condition, where obj.a = {} is the only change registered by observer, apparently since the new object isn't turned into an observable before the child objects are added to it, unless I force the subsequent changes to be registered immediately by calling deliverChanges().

kurtmilam avatar Jul 25 '14 14:07 kurtmilam

I have installed latest obsered module in my node code my code is: var observed = require('observed'); var result = {}; var e = observed(result);

observed(result); throws error that TypeError: Object function Object() { [native code] } has no method 'observe' at Observable._bind (/Applications/XAMPP/xamppfiles/htdocs/citinode/node_modules/observed/lib/observed.js:77:10) at new Observable (/Applications/XAMPP/xamppfiles/htdocs/citinode/node_modules/observed/lib/observed.js:41:8) at Observable (/Applications/XAMPP/xamppfiles/htdocs/citinode/node_modules/observed/lib/observed.js:36:12)

how can i overcome this error?

Paras-avantsoft avatar Dec 25 '14 13:12 Paras-avantsoft

@Paras-avantsoft which version of node are you using? You need node >=0.11.13 because a lower version does not include ECMAS7 Also, i don't see how this is related to the issue discussed here, so please open a new issue.

Wortex17 avatar Dec 28 '14 12:12 Wortex17