blittle / backbone-nested-models

Backbone Nested Models
86 stars 28 forks source link

normalizing "parse" behavior #35

Closed jascha-schiffer closed 6 years ago

jascha-schiffer commented 6 years ago

parse gets executed on related collections (within backbone), for related models we need to call parse on our own if the parse option is provided

This results inconsistent behavior when fetching data from a server and can be reproduced locally

var TestModel = Model.extend({
    defaults: {
        parsed: false,
    },
    parse(attr) {
        attr.parsed = true;
        return attr;
    }
});

var TestCollection = Collection.extend({
    model: TestModel
});

var RootModel = Model.extend({
    defaults: {
        testModel: null,
        testCollection: [],
    },
    relations: {
        testModel: TestModel,
        testCollection: TestCollection,
    },
});

var testInstance = new RootModel();
console.log(testInstance.toJSON()); // { testModel: { parsed: false }, testCollection: [] }

testInstance.set({
    testModel: {blub: "bla"},
    testCollection: [
        {blub: "bla2"},
        {blub: "bla3"}
    ]
}, {parse: true});

console.log(testInstance.toJSON()); // { testModel: { parsed: false, blub: 'bla' }, testCollection: [ { parsed: true, blub: 'bla2' }, { parsed: true, blub: 'bla3' } ] }
blittle commented 6 years ago

@jascha-schiffer Thank you for the contribution. I haven't had time to actively maintaining this project anymore. I'll merge the PR though and republish a version trusting in your changes :)