Backbone relational - cannot instantiate more than one ,,,
Asked Answered
P

3

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?

Pitcher answered 21/9, 2012 at 15:2 Comment(3)
I had that problem... I don't knnow why but i deleted the reverse relation and problem solve by the moment...Rivy
Ahh. I didn't configure the reverse relations...Pitcher
Take a look at this: #12224622Koon
P
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

Play answered 4/7, 2013 at 6:49 Comment(1)
This would work but the models are being fetched dynamically, by the Collections.Pitcher
D
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.

Delegacy answered 10/6, 2013 at 22:42 Comment(0)
A
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.

Axolotl answered 17/1, 2013 at 14:22 Comment(1)
I'm not quite sure what you mean... You mean before using Collection.fetch(), I should first instantiate a model?Pitcher

© 2022 - 2024 — McMap. All rights reserved.