relational cannot instantiate two RelationalModel objects

I am trying to implement BackboneRelational and keep getting

"Cannot instantiate more than one Backbone.RelationalModel with the same id per 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 extends Backbone.Model I can instantiate both, but I get all the warnings that the relational functionality is broken..

I am trying to achieve the following:

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

What am I missing?


The general idea behind the "one model per id" situation is that Backbone Relational uses a data store ( Backbone.Relational.store ) to eliminate repeated requests for models that have already been loaded.

Fortunately, it also provides a few helpers to help access models through the store. Instead of supplying an ID and fetching the plot, you might instead use the findOrCreate method you'll find attached to App.Models.Plot :

plot = App.Models.Plot.findOrCreate(700)
user = plot.get('user')
链接地址: http://www.djcxy.com/p/65520.html

上一篇: 关系型FetchRelated不会获取任何东西

下一篇: relational不能实例化两个RelationalModel对象