Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Trigger a 'collision' event when there's an collision by ids. #295

Closed
wants to merge 1 commit into from

3 participants

@shoe

This is to open a discussion whether there's really a bug here, and if so, how to fix it.
This patch is what I'm currently using to resolve the issue, but it might not be a good general solution.

The problem:
BBR claims to rely on keeping only a single instance of each model in it's own storage collections. However, in at least one case, it would silently replace a model with a newer copy. This causes problems since the orphaned "old" copy can still be alive in other collections. BBR will now at least trigger a 'collision' event when this happens, which allows users to handle the collision intelligently, e.g. by destroying the old copy.

This collision can arise from the following sequence:

a = new MyRelationalModel() // This object starts with no id.
b = new MyRelationalModel({'id': 42})  // This object starts with an id of 42.
a.set('id', 42)

Should this be allowed to happen? Doesn't this violate some fundamental assumptions made by BBR? What should happen in this case? This patch takes the approach that only the model itself will know how to appropriately resolve the conflict.

Let me know if you'd like more detail.

@PaulUithol
Owner

This definitely looks like something that shouldn't be allowed to happen. We should probably make this an error right away; this shouldn't be ignored (almost) silently, and an error makes it easiest to track where something went wrong.

@shoe

I thought about making it throw an error right away, but then I couldn't think of anyway to prevent it. And I did have an easy way of dealing with the condition if I knew when it happened. That's why triggering a 'collision' event works for me.

@PaulUithol
Owner

Then you should catch the error? If this situation occurs, relations on one or both of these models WILL be broken, and there's no way to tell on which they'll break, or in what way. Therefore, an error would be the only way out here. We'll need to get out before the duplicate id is actually set.

@shoe

@PaulUithol I don't understand why you prefer throwing an error in this case, rather than triggering an event. I don't see how I can catch this error without modifying backbone itself, since it's backbone itself that calls set() from within the success callback for the save() function (before any user-supplied hooks, too).

Here's the code from backbone:

options.success = function(resp) {
    // Ensure attributes are restored during synchronous saves.
    model.attributes = attributes;
    var serverAttrs = model.parse(resp, options);
    if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs);
    if (_.isObject(serverAttrs) && !model.set(serverAttrs, options)) {
      return false;
    }
    if (success) success(model, resp, options);
    model.trigger('sync', model, resp, options);
  };

And here's the stacktrace where it triggers this error:

Uncaught Error: Cannot instantiate more than one Backbone.RelationalModel with the same id per type! 

_.extend.checkId backbone-relational.js:429
Backbone.RelationalModel.Backbone.Model.extend.set backbone-relational.js:1366
_.extend.save.options.success backbone.js:482
jQuery.Callbacks.fire jquery.js:1038
jQuery.Callbacks.self.fireWith jquery.js:1149
done jquery.js:8075
jQuery.ajaxTransport.send.callback jquery.js:8599

If BBR triggered an event, I could handle the conflict, but how could I recover when it throws this error (assuming I don't want to patch backbone.js)?

@dbergey

I also am running into the issue in this case: Backbone is calling .set within the .fetch success callback. My assumption was that new versions with the same ID being added to a collection would replace or update the old version. Is there a way we can make sure this happens instead of just dying?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 0 deletions.
  1. +3 −0  backbone-relational.js
View
3  backbone-relational.js
@@ -424,6 +424,9 @@
*/
update: function( model ) {
var coll = this.getCollection( model );
+ var other = coll.get( model );
+ if (other && other !== model)
+ model.trigger('collision', other);
coll._onModelEvent( 'change:' + model.idAttribute, model, coll );
},
Something went wrong with that request. Please try again.