Backbone-relational icon indicating copy to clipboard operation
Backbone-relational copied to clipboard

Auto fetch related models

Open gjdass opened this issue 10 years ago • 6 comments

Hi.

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.

gjdass avatar Mar 29 '14 01:03 gjdass

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.

PaulUithol avatar Apr 01 '14 08:04 PaulUithol

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 : http://jsfiddle.net/gjdass/WNWrm/

gjdass avatar Apr 05 '14 03:04 gjdass

UP ?

gjdass avatar Apr 28 '14 19:04 gjdass

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 avatar Aug 28 '14 10:08 cosminnus

@cosminnus your fix really works :+1:

alexandrebini avatar Nov 16 '14 16:11 alexandrebini

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

marcovc avatar Sep 06 '15 00:09 marcovc