GTF
GTF

Reputation: 8395

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:

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?

Upvotes: 7

Views: 2278

Answers (3)

zzz
zzz

Reputation: 103

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

Upvotes: 2

Marc Greenstock
Marc Greenstock

Reputation: 11668

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.

Upvotes: 1

mingle
mingle

Reputation: 1617

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.

Upvotes: 0

Related Questions