Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Instantiating same ID with the same class #305

Open
fertapric opened this Issue · 2 comments

2 participants

@fertapric

Hi all,

I'm able to instantiate more than one object of the same type with the same ID. I attach the screenshot:

Screen Shot 2013-03-21 at 19 30 17

This happens when I fetch a collection of "Snippets" and after that I fetch one of the objects using a new Snippet object.

To give more context, the API I'm using looks like Twitter API, and API calls look like:

List collection of Snippets: /snippets/created/by/:user_id
Detail of snippet: /snippets/show/:snippet_id

So, to get the information of a Snippet:

new Snippet({ _path: 'show/' + model.id }).fetch({
  success: function(model, response) {
    ...
  }
});

I was expecting that once the server return the same Snippet object, a new object wouldn't be created, but the result is that a new object is added to the Backbone.Relational.store collections.

I saw that I can "solve" the problem using something like this:

var snippet = Snippet.findOrCreate(model.id) || new Snippet();
snippet._path = 'show/' + model.id;
snippet.fetch({
  success: function(model, response) {
    ...
  }
});

but I thought it would be nice to report this.

@PaulUithol
Owner

Looks like what you're doing is similar to #295 ? Create an "empty" model, then set the id?

@fertapric

Exactly. I will follow up the other issue.
Thanks

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.