Question

I am having some trouble when I create a new model form json, with nested attributes that should be parsed as a collection. Here I show how I get to the error:

(Any help appreciated)

1) I create a user like this

user = new App.Models.User( {"email":"tom@gmail.com","id":1,"user_cities_attributes":[{"city_id":431,"user_action":"to-visit","user_id":1}] }

2) and then I check in the console what happened, the collection did not get created. :S

user.get('user_cities')
> undefined # where is my collection?! :(
user.get('user_cities_attributes')
> [Object] # mmm weird...

If I try after to add a new model to the collection...

cities = new App.Collections.CitiesCollection()
cities.fetch()

userCity = new App.Models.UserCity( { user: user, city: cities.first(), user_action: "to-visit" } )

now everything seems to be fine... what happened????

user.get('user_cities')
> Backbone.Collection {length: 20, models: Array[20], _byId: Object, model: function, user: User…}
user.get('user_cities_attributes')
undefined

MODELS

class App.Models.User extends Backbone.RelationalModel
  paramRoot: 'user'
  urlRoot: 'users'

  defaults:
    name: null

class App.Models.UserCity extends Backbone.RelationalModel
  paramRoot: 'user_city'
  urlRoot: 'user_cities'

  defaults:
    user_action: null

  relations: [{
    type: Backbone.HasOne,
    key: 'user',
    relatedModel: 'App.Models.User',
    collectionType: 'App.Collections.UsersCollection',
    keySource: 'user_id',
    includeInJSON: 'id',
    reverseRelation: {
      key: 'user_cities',
      keySource: 'user_cities_attributes',
      includeInJSON: 'id'
    }
  },
  {
    type: Backbone.HasOne,
    key: 'city',
    relatedModel: 'App.Models.City',
    collectionType: 'App.Collections.CitiesCollection',
    includeInJSON: 'id',
    keySource: 'city_id',
    reverseRelation: {
      key: 'users',
      includeInJSON: 'id'
    } 
  }]

class App.Models.City extends Backbone.RelationalModel
  paramRoot: 'city'
  urlRoot: 'cities'

  defaults:
    name: null

  relations: [{
    type: Backbone.HasMany,
    key: 'places',
    relatedModel: 'App.Models.Place',
    collectionType: 'App.Collections.PlacesCollection',
    keySource: 'places_attributes',
    # parse: true,
    includeInJSON: 'id',
    autoFetch: true,
    reverseRelation: {
      key: 'city',
      includeInJSON: 'id'
    }
      # 'relatedModel' is automatically set to 'Zoo'; the 'relationType' to 'HasOne'.
  }]
Was it helpful?

Solution

Look at the note about reverseRelation and using coffeescript (from: http://backbonerelational.org/#relations-reverseRelation):

Note that if you define a relation (plus a reverseRelation) on a model, but don't actually create an instance of that model, it is possible initializeRelations will never get called, and the reverseRelation will not be initialized. This can happen when extend has been overridden, or redefined as in CoffeeScript. See setup.

Then try using .setup as described here: http://backbonerelational.org/#RelationalModel-setup

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top