PaulUithol / Backbone-relational

Get and set relations (one-to-one, one-to-many, many-to-one) for Backbone models
http://backbonerelational.org
MIT License
2.34k stars 330 forks source link

initial fetch() does not pull related objects #552

Open visigoth opened 8 years ago

visigoth commented 8 years ago

i've got a model that i need to fetch from a known URL from the server. the model has a HasMany relation within it. on creation, i've got an identifier and i then invoke fetch() to grab it from the server. i would have expected backbone-relational to grab the relationship for me, but per https://github.com/PaulUithol/Backbone-relational/blob/master/backbone-relational.js#L1235, the override for trigger doesn't react to the sync event, which doesn't make sense to me: if the parent model has been fetched from the server, the relations should get fetched as well. am i missing something? i've got an override for Backbone.sync, but looking at the code for backbone-relational, it doesn't look like it would interfere.

let User = Backbone.RelationalModel.extend({ relations: [{ type: Backbone.HasMany, key: 'books', relatedModel: Book, includeInJSON: Backbone.Model.prototype.idAttribute, collectionType: Books, }] });

let user = new User({id: 4}); user.fetch().then(() => { console.log(user.books); }); // should be an array of identifiers?

mr-feek commented 8 years ago

Isn't this what the autoFetch key is for? It is defaulted to false, so toggling that to true should do what you are looking for (though I haven't tested).

Abdillah commented 7 years ago

I tried to set the autoFetch: true, but it doesn't fetch it's relation model. After look on getAsync source code and test it out, seems the problem is, it stops after knowing the idsToFetch is empty. What other options should I supply?