Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactor recursion doesn't work #2

Closed
Simcon opened this issue May 2, 2013 · 4 comments
Closed

Reactor recursion doesn't work #2

Simcon opened this issue May 2, 2013 · 4 comments

Comments

@Simcon
Copy link

Simcon commented May 2, 2013

The following test case for recursion doesn't appear to work (ko 2.2.1, ko.mapping 2.4.0):

var data = "{\"id\":1,\"items\":[{\"id\":1},{\"id\":2}]}";

var model = ko.mapping.fromJSON(data);

ko.watch(model, { recurse: true }, function () {
    alert('Reactor');
});

// Change the data
model.id(2); // Reacts OK
model.items()[0].id(2); // Does not react
@ZiadJ
Copy link
Owner

ZiadJ commented May 13, 2013

Thanks for the suggestion.

I quickly modified the code to get it to work in your case. To test it you need to replace lines 62 to 72 with this:

function watchChildren(object, recurse) {
    if (recurse && (recurse === true || recurse >= 0)) {
        if (typeof object === 'object') {
            watchObjectOrArray(object, recurse === true || Number(recurse) - 1);
        } else if (ko.isSubscribable(object)) {
            if (object() instanceof Array)
                watchObjectOrArray(object(), recurse === true || Number(recurse) - 1);
        }
    } else if (ko.isSubscribable(object)) {
        context.watch(object, options, valueEvaluatorFunction);
    }
}

function watchObjectOrArray(object, recurse) {
    // Listen to all subscribables within specified object or array.
    if (typeof object === 'object') {
        for (var property in object)
            watchChildren(target[property], recurse);
    } else if(object instanceof Array) {
        for (var i = 0; i < object.length; i++)
            watchChildren(object[i], recurse);
    }
}

options.targetParent = target;

watchObjectOrArray(target, options.recurse);

Please let me know your thoughts about it and I'll update the source once it passes your tests.

@grennis
Copy link

grennis commented May 17, 2013

I don't think this works correctly when items are added or removed from the child array.

@ZiadJ
Copy link
Owner

ZiadJ commented May 19, 2013

I've put up a beta version of the code that supports it here:
https://github.com/ZiadJ/knockoutjs-reactor/blob/master/knockout.reactor.latest.js

Sorry I haven't had much time to test it yet but feel free to let me know if you come across any bugs.

@ZiadJ
Copy link
Owner

ZiadJ commented Jan 26, 2014

It's been a while since I've last been around and I've finally fixed the issue. I've tested it against large and complex object graphs generated by BreezeJS. So everything should work fine now as long as the depth value allows it to reach your nested elements.

P.S recurse has been replaced by depth in the latest version.

@ZiadJ ZiadJ closed this as completed Jan 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants