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

Object references seem to get broken after collection.set #67

Closed
gregtillbrook opened this issue Sep 19, 2013 · 6 comments
Closed

Object references seem to get broken after collection.set #67

gregtillbrook opened this issue Sep 19, 2013 · 6 comments
Assignees
Labels

Comments

@gregtillbrook
Copy link

Hi, we've been evaluating bb-associations for use in our webapp but think there's an issue with objects being recreated when setting via blobs of json. Ive seen issue #31 but think there's still a problem there. Here's a test case that I believe demonstrates the problem ;

    test("nested set shouldnt destroy references", function () {

        var Team = Backbone.Collection.extend({
                model:Employee
            }),
            json1 = [{
                    id:'e1',
                    fname: "John",
                    manager: {
                        id:'m1',
                        fname:'Mikeeee'
                    }
                }, {
                    id:'e2',
                    fname: "Edgar"
                }],
            json2 = [{
                    id:'e1',
                    fname: "John",
                    manager: {
                        id:'m1',
                        fname:'Mike' //<----- changed
                    }
                }, {
                    id:'e2',
                    fname: "Edgar"
                }];

        var team = new Team(json1);
        var employee1 = team.at(0);
        var manager1 = employee1.get('manager');

        team.set(json2);
        //re-resolving reference will get new value as expected
        equal(employee1.get('manager.fname'), 'Mike');
        //Fails: direct reference to manager object will return old value
        equal(manager1.get('fname'), 'Mike');

    });

To give a bit more context - We have a rich model/collection hierarchy thats; a) used to drive a number of nested Marionette CollectionViews/CompositeViews etc and b) can be periodically updated in part or whole by large blobs of json. So it's important that references aren't broken to avoid UI re-draws.

Any thoughts?
Cheers,
Greg

@gregtillbrook
Copy link
Author

I realised the test case didn't capture the problem correctly so have updated it.

Im beginning to suspect the problem is due to the base object being a Collection rather than a Model - sound plausible?

@gregtillbrook
Copy link
Author

I tried an alternate test that wraps the structure in a Model (as a possible workaround) but this also fails.

    test("nested set shouldnt destroy references - take 2", function () {

        var Employees = Backbone.Collection.extend({
                model:Employee
            }),
            Team = Backbone.AssociatedModel.extend({
                relations: [
                    {
                        type: Backbone.Many,//nature of the relation
                        key: 'employees',
                        collectionType: Employees, //Collection to be used.
                        relatedModel: Employee //Optional
                    }
                ],
                defaults: {
                }
            }),
            json1 = {
                id:'t1',
                employees:[
                    {
                        id:'e1',
                        fname: "John",
                        manager: {
                            id:'m1',
                            fname:'Mikeeee'
                        }
                    }
                ]
            },
            json2 = {
                id:'t1',
                employees:[
                    {
                        id:'e1',
                        fname: "John",
                        manager: {
                            id:'m1',
                            fname:'Mike' //<----- changed
                        }
                    }
                ]
            };

        var team = new Team(json1);
        var manager1 = team.get('employees[0].manager');

        team.set(json2);
        //re-resolving reference will get new value as expected
        equal(team.get('employees[0].manager.fname'), 'Mike');
        //Fails: direct reference to manager object will return old value
        equal(manager1.get('fname'), 'Mike');

    });

@ghost ghost assigned dhruvaray Sep 20, 2013
dhruvaray added a commit that referenced this issue Sep 20, 2013
@dhruvaray
Copy link
Owner

Thanks for the elaborate test case(s) and catching this. d13243c fixes the scenarios you have submitted. They are now part of the test suite as well. Let me know if it works for you.

@gregtillbrook
Copy link
Author

Wow, and thank you very much for what is probably the quickest fix Ive ever received for an issue Ive raised :D !

Works great.

@dhruvaray
Copy link
Owner

Thank you! If you are curious, the fix essentially involved not creating a new model when a model with the same id value is passed from outside. It was handled for collections in issue #31. But I had somehow missed this case. I took the opportunity to simply the logic while I was fixing this case.

Lastly, if you like our library, please don't forget to share your appreciation by starring us!

@gregtillbrook
Copy link
Author

All happy + starred.

Cheers,
Greg

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

No branches or pull requests

2 participants