Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Bug when watching new properties #108

Open
bloodcarter opened this issue Feb 20, 2017 · 2 comments
Open

Bug when watching new properties #108

bloodcarter opened this issue Feb 20, 2017 · 2 comments
Labels

Comments

@bloodcarter
Copy link

I tried your example from official docs, but it doesn't catch any changes. Here the full code:

'use strict';

var WatchJS = require("watchjs")
var watch = WatchJS.watch;
var unwatch = WatchJS.unwatch;
var callWatchers = WatchJS.callWatchers;

var ex = {
    l1a: "bla bla",
    l1b: {
        l2a: "lo lo",
        l2b: "hey hey"        
    }
};

watch(ex, function (prop, action, difference, oldvalue){

    console.log("prop: "+prop+"\n action: "+action+"\n difference: "+JSON.stringify(difference)+"\n old: "+JSON.stringify(oldvalue)+"\n ... and the context: "+JSON.stringify(this));    

}, 0, true);


ex.l1b.l2c = "new attr"; //it is not instantaneous, you may wait 50 miliseconds

setTimeout(function(){
    ex.l1b.l2c = "other value";
}, 100);
@melanke
Copy link
Owner

melanke commented Mar 15, 2018

This implementation of watching new values isn't recommended, and I just realised it has a bug, probably will be remade in the next version.

You should use like this:
http://jsfiddle.net/1jwntatp/

//defining our object no matter which way we want
var ex = {
    l1a: "bla bla",
    l1b: {
        l2a: "lo lo",
        l2b: "hey hey",
        l2c: null
    }
};

watch(ex, function (prop, action, difference, oldvalue){
    
    alert("prop: "+prop+"\n action: "+action+"\n difference: "+JSON.stringify(difference)+"\n old: "+JSON.stringify(oldvalue)+"\n ... and the context: "+JSON.stringify(this));    
    
});


ex.l1b.l2c = "new attr";

@melanke melanke added the bug label Mar 15, 2018
@melanke melanke changed the title Your example not working on node js v7.2.1 Bug when watching new properties Mar 15, 2018
@kolorafa
Copy link

The example: "Do you want to know when new attributes change too?" works but somehow not if you have level set o 0, if you set it to eg. 10 it works. (At least in browser)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants