Skip to content

initial fetch() does not pull related objects #552

@visigoth

Description

@visigoth

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions