dhruvaray / backbone-associations

Create object hierarchies with Backbone models; Respond to hierarchy changes using regular Backbone events.
http://dhruvaray.github.io/backbone-associations/
MIT License
492 stars 75 forks source link

Mapping models and collections from fetch calls parse when it should not. #147

Open steelsojka opened 10 years ago

steelsojka commented 10 years ago
var MyModel = Backbone.AssociatedModel.extend({
    relations: [{
        type: Backbone.Many,
        key: "entities",
        collectionType: EntityCollection
    }],
    parse: function(res) {
         // This gets called twice. When the response comes back
         // and then when setting the `entities` key
         return res.entities;
    },
    url: "myUrl"
});

model = new MyModel();

model.fetch();

When backbone-associations calls set when creating the EntityCollection with the key of entities it passes the options from the fetch which Backbone automatically sets the parse option to true which calls the parse function when setting the collection. In this case this would throw an undefined error since parse is getting called with just the array.

I'm not sure if this is a Backbone specific issue or not. Has anybody else ran into this issue?

steelsojka commented 10 years ago

After digging through the source code, I realized I can add options onto the relation. I'll try this and see if it works.

steelsojka commented 10 years ago

My above comment did not work since the options passed in override relation options :(

steelsojka commented 10 years ago

The problem is that passed in options still override relation options. https://github.com/dhruvaray/backbone-associations/blob/master/backbone-associations.js#L271

Should this be the other way around?