Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

relational:change is always fired by Backbone.RelationalModel.updateRelations #140

Closed
wkleinheerenbrink opened this Issue · 2 comments

3 participants

@wkleinheerenbrink

Hi There,

In my project I'm using a 1-n relation, say between one model A and many B's. Now when I do A.fetch(), the model is updated as expected. The problem is that even when nothing has changed, the complete collection of B's is rebuild (firing removes and adds). After same stacktracing I found out the 'relational:change' method is called for every related model; even when there is no change. The current code is:

_.each( this._relations, function( rel ) {
    var val = this.attributes[ rel.key ];
    if ( rel.related !== val ) {
        this.trigger( 'relational:change:' + rel.key, this, val, options || {} );
    }
}, this );

My stacktrace states that val is an array with a list of new elements while rel.related is a model. The check rel.related !== val will therefore always be false? Is this because I've configured something wrong or is this a bug in RelationalModel?

I'm using the followings libs:

  • Backbone
  • jQuery
  • Backbone Relational
  • Backbone TastyPie

Wout

@DouweM
Collaborator

Events have been refactored recently and should now behave more sensibly. If this issue still exists, let us know.

@DouweM DouweM closed this
@1ex
1ex commented

Hello!

I have exactly the same problem like in first post having backbone-relational v0.7.1. Here is the current code of updateRelations:

        updateRelations: function( options ) {
            if ( this._isInitialized && !this.isLocked() ) {
                _.each( this._relations || [], function( rel ) {
                    // Update from data in `rel.keySource` if set, or `rel.key` otherwise
                    var val = this.attributes[ rel.keySource ] || this.attributes[ rel.key ];
                    if ( rel.related !== val ) {
                        this.trigger( 'relational:change:' + rel.key, this, val, options || {} );
                    }
                }, this );
            }
        },

My model:

Place = Backbone.RelationalModel.extend({

   relations: [
   {
      type: Backbone.HasMany, // Use the type, or the string 'HasOne' or 'HasMany'.
      key: 'childPlaces',      
      keySource: 'childPlacesIds',
      relatedModel: 'Place',   
      collectionType: 'Places'
   },    

});      

After doing place.fetch() condition "rel.related !== val" is always true, because "rel.related" is instance of model, but val is array of "ids". Here is snapshot from chrome developer tools with breakpoint fired on "if (rel.related !== val)" line

 - 06 02 2013 - 02:23:31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.