Backbone relational communication cannot create two RelationalModel objects

I try to implement BackboneRelational and keep getting

"It is not possible to instantiate more than one Backbone.RelationalModel with the same identifier for each type!"

class App.Models.User extends Backbone.RelationalModel urlRoot : '/api/users' idAttribute: 'id' relations: [ type: Backbone.HasMany key: 'plots' relatedModel: 'App.Models.Plot' collectionType: 'App.Collections.Plots' includeInJSON: false reverseRelation: key: 'user_id', includeInJSON: 'id' ] class App.Models.Plot extends Backbone.RelationalModel urlRoot : '/api/plots' idAttribute: 'id' 

If I switch one of the models to the Backbone.Model extension, I can create an instance of both, but I get all warnings that the relational functionality is not working.

I am trying to achieve the following:

  plot = new App.Models.Plot({id : 700}) plot.fetch() plot.get('user') 

What am I missing?

+7
source share
1 answer

The general idea of ​​the β€œone model for one” situation is that Backbone Relational uses a data warehouse ( Backbone.Relational.store ) to eliminate duplicate queries for already loaded models.

Fortunately, it also provides several assistants to help access models through the store. Instead of specifying an identifier and retrieving the plot, you can use the findOrCreate method findOrCreate , which you will find attached to App.Models.Plot :

 plot = App.Models.Plot.findOrCreate(700) user = plot.get('user') 
+10
source

All Articles