New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
3 participants
@shoe
Contributor

shoe commented Mar 18, 2013

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

This comment has been minimized.

Show comment
Hide comment
@PaulUithol

PaulUithol Mar 18, 2013

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.

Owner

PaulUithol commented Mar 18, 2013

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

This comment has been minimized.

Show comment
Hide comment
@shoe

shoe Mar 19, 2013

Contributor

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.

Contributor

shoe commented Mar 19, 2013

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

This comment has been minimized.

Show comment
Hide comment
@PaulUithol

PaulUithol Mar 23, 2013

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.

Owner

PaulUithol commented Mar 23, 2013

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

This comment has been minimized.

Show comment
Hide comment
@shoe

shoe Apr 13, 2013

Contributor

@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)?

Contributor

shoe commented Apr 13, 2013

@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

This comment has been minimized.

Show comment
Hide comment
@dbergey

dbergey Jun 11, 2013

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?

dbergey commented Jun 11, 2013

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