Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone relational - cannot instantiate more than one ,,,

I have been using backbone relational (https://github.com/PaulUithol/Backbone-relational) to build my application because I have a model (Room) with lots of other models attached:

  • Room has many Comment
  • Room belongs to Location

As you can see in this issue I keep getting the error

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

Whenever I change View or reload a Collection.

Now I know there is a thread here where they suggest using findOrCreate but I am not accessing models individually, but am instead using Collection.fetch. Is there a way around this problem?

like image 702
GTF Avatar asked Sep 21 '12 15:09

GTF


2 Answers

with backbone-relational you can get or create object from relational store. Use this to create relational objects:

this.model = someModel.findOrCreate({id: 123})

With this it will create new object or get existing one from relational store.

http://backbonerelational.org/#RelationalModel-findOrCreate

like image 186
zzz Avatar answered Sep 19 '22 13:09

zzz


I had a similar problem, fixed it by switching to Backbone-associations.

https://github.com/dhruvaray/backbone-associations

The interface is almost exactly the same with only a few minor differences so porting should be pretty simple.

like image 29
Marc Greenstock Avatar answered Sep 18 '22 13:09

Marc Greenstock