0

using the browser side version of watch.js.

in the html head:

<script type="text/javascript" src="S/watch.js"></script>

In the javascript:

var here={'now':'somewhere'}

watch(here,['now'],function(){
  console.log('home changed: '+here.now);
  });

here.now='somewhere else';

I get this error looping over and over and over:

Uncaught TypeError: Cannot read property 'undefined' of undefined watch.js:345

the error show to be coming from here - watch.js code:

var loop = function(){

        for(var i in lengthsubjects){

            var subj = lengthsubjects[i];
            var difference = getObjDiff(subj.obj[subj.prop], subj.actual);   <-watch.js:345

why does this happen?

Update

Even if I watch nothing and only add the script to the head it still does the same errors infinatly

Ben Muircroft
  • 2,936
  • 8
  • 39
  • 66
  • What browser are you using ? – Gabriele Petrioli Sep 02 '13 at 14:18
  • google chrome Version 30.0.1599.10 dev – Ben Muircroft Sep 02 '13 at 14:20
  • Your `here` var is in JSON notation. You should use `"` instead of `'` for standard compliance. Don't know if that impacts the `watch.js` library, but its worth a shot, I'd say... – Joum Sep 02 '13 at 14:32
  • thanks I did not know that... (no change in error though) – Ben Muircroft Sep 02 '13 at 14:36
  • 1
    @Joum where did you get that? There's absolutely no difference between them. – user247702 Sep 02 '13 at 14:49
  • look at the second answer here: http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript – Joum Sep 03 '13 at 07:42
  • @Joum it's a JavaScript object literal, not JSON. [JSON and the JavaScript syntax construct commonly known as "object literal"](http://meta.stackexchange.com/questions/194280/preventing-misinformation-correcting-the-usage-of-the-term-json-object) – user247702 Sep 03 '13 at 09:11
  • @Stijn Good call, point taken! Thank you! :) even so, I believe that the point raised by the second answer in the link I posted seems important, although it apparently does not relate to this question. – Joum Sep 03 '13 at 12:43

1 Answers1

0

FIXED!

in the loop function wrap everything in this

if(lengthsubjects.length!==0){}

because if you do console.dir(lengthsubjects);

you get:

length: 0,
__proto__: Array[0]

here is the change in watch.js from lines 340-360:

var loop = function(){
    if(lengthsubjects.length!==0){
    for(var i in lengthsubjects){
        //
        var subj = lengthsubjects[i];
        var difference = getObjDiff(subj.obj[subj.prop], subj.actual);

        if(difference.added.length || difference.removed.length){
            if(difference.added.length){
                for(var j in subj.obj.watchers[subj.prop]){
                    watchMany(subj.obj[subj.prop], difference.added, subj.obj.watchers[subj.prop][j], subj.level - 1, true);
                }
            }

            callWatchers(subj.obj, subj.prop, "differentattr", difference, subj.actual);
        }
        subj.actual = clone(subj.obj[subj.prop]);

    }

    }
};
Ben Muircroft
  • 2,936
  • 8
  • 39
  • 66