melanke / Watch.JS

watch the changes of any object or attribute

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

changing a value on a watched object and calling callWatchers does not update newValue

grofit opened this issue · comments

commented

If I were to do the following: (here is a jsfiddle https://jsfiddle.net/ocwj075r/1/)

var something = { foo: "hello"  };
watch(something, function(prop, action, newValue, oldValue){
   console.log(arguments);
});

something.foo = 10;
callWatchers(something);

outputs:

'model changed', Object{0: 'foo', 1: undefined, 2: undefined, 3: undefined} // from manual update
'model changed', Object{0: 'foo', 1: 'set', 2: 10, 3: 'hello'} // from normal loop notify

I was expecting:

A) It would only raise a single update
B) The new value would contain the value of the object

So is this a bug or is this expected behaviour as I was expecting callWatchers to manually check for updates outside of the loop? assuming you needed to expedite a change rather than waiting the 50ms.

I also thought it didn't work but you need to pass a second argument that is a string of one of the properties not sure if it matters that it is a property but it does need the second argument to actually call the watcher.

commented

ah ok, its a moot point for me now as I ended up writing my own model watcher as I also needed full path resolution of property names, but good to know this is how it works.