7

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?

Community
  • 1
  • 1
GTF
  • 8,031
  • 5
  • 36
  • 59
  • I had that problem... I don't knnow why but i deleted the reverse relation and problem solve by the moment... – Edgar Nadal Jan 11 '13 at 01:47
  • Ahh. I didn't configure the reverse relations... – GTF Jan 12 '13 at 19:13
  • Take a look at this: http://stackoverflow.com/questions/12224122/backbone-relational-cannot-instantiate-two-relationalmodel-objects#answer-12224192 – macool Jun 06 '13 at 16:36

3 Answers3

2

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

zzz
  • 103
  • 1
  • 11
  • 1
    This would work but the models are being fetched dynamically, by the Collections. – GTF Jul 05 '13 at 18:32
1

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.

Marc Greenstock
  • 11,278
  • 4
  • 30
  • 54
0

I had similar problem and it happend that I need to initialize model with function:

model: ->

   return new Project.Models.ModelName()

When you require_tree . in application.js then model files are loaded later than collection files (alphabetical order). Setting model with function waits until the app is executed.

mingle
  • 1,567
  • 1
  • 16
  • 19
  • I'm not quite sure what you mean... You mean before using `Collection.fetch()`, I should first instantiate a model? – GTF Jan 18 '13 at 19:51