Auto fetch related models #448

gjdass opened this Issue Mar 29, 2014 · 6 comments

6 participants



I got an huge wtf problem that i can't figure out. I explain :

I have a model named Product :

var Product = Backbone.RelationalModel.extend(
    urlRoot: Backbone.rootApiUrl + '/products',
    defaults: {
        id: '',
        name: '',
        description: '',
        current_price: '',
        categories: '',
        duration: '',
        shipping_cost: '',
        start_date: '',
        user_id: null,
        is_buy_it_now: ''

    relation: [{
        type: Backbone.HasOne,
        key: 'user_id',
        relatedModel: User,
        autoFetch: true

My field 'user_id' is a foreign key for one User (which is also a Model in Backbone) :

var User = Backbone.RelationalModel.extend(
    urlRoot: Backbone.rootApiUrl + '/users',
    defaults: {
        id: '',
        name: '',
        email: '',
        firstname: '',
        lastname: '',
        password: '',
        card_nb: '',
        cart_total: ''

My fetch relations work perfectly in each separate model. But when i put a relation between the Product.user_id and my User model, a wtf error occurs.

I've tried everything possible : all the keys (keySource, keyDestination ........... with all values possibles). I've even put breakpoints into the Relation Lib to be able to watch what happened ... Nothing to say. Even StackOverflow doesn't know this bug :P (Or i badly searched)

The final problem is :

  • My product is correctly sent by the API and set in Backbone (OK)
  • The field "user_id" is first set at a number by the Product API response, and then put at NULL by Relational instead of being replaced by the object User (WHY ?)
  • When i watch my network debugger, Relational call the API for the User object (it's all right here) with the "user_id" number (OK). The server response is good, with the right user in JSON (OK). But Relational doesn't bind this response object with the field "user_id", which is now .... NULL (NOT OK).
    • So i got an User object lost (but i can console.log() it if i put a success function in my autoFetch), not correctly linked to his parent Product with the field "user_id".

Thanks in advance :/ Sorry for the time you may take for this.

@PaulUithol PaulUithol changed the title from Auto fetch sub-models to Auto fetch related models Apr 1, 2014

Could you try to translate what you're seeing to a jsFiddle? Sort of sounds to me like there's a data problem somewhere in there - but I can't figure out where with this description.

Just creating models using a piece of JSON shouldn't behave differently from fetching them.


Hi :)

Thanks for the answer. Sorry for my answer-lag, but i had to find how JSFiddle works (i didn't use it until now).

Here is the link with my complete code working :


UP ?


I had the same problem a few days ago and fixed it by making a small change inside the Relation constructor. Listeners should be defined before calling the fetchRelated call:

        this.listenTo( this.instance, 'destroy', this.destroy )
            .listenTo( this.relatedCollection, 'relational:add relational:change:id', this.tryAddRelated )
            .listenTo( this.relatedCollection, 'relational:remove', this.removeRelated);

        if ( this.options.autoFetch ) {
            this.instance.fetchRelated( this.key, _.isObject( this.options.autoFetch ) ? this.options.autoFetch : {} );

@cosminnus your fix really works 👍


I was unable to use autoFetch at all (same problem described above), but the fix by @cosminnus made it work. I hope the fix goes into code. Thanks! Marco

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment