Skip to content
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

Instantiating same ID with the same class #305

Closed
fertapric opened this issue Mar 21, 2013 · 2 comments
Closed

Instantiating same ID with the same class #305

fertapric opened this issue Mar 21, 2013 · 2 comments

Comments

@fertapric
Copy link

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
Copy link
Owner

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

@fertapric
Copy link
Author

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants