In the following code, I establish 3 has-many/belongs-to relations.
Category > Subcategories > Items
Category.js.coffee:
class App.Models.Category extends Backbone.RelationalModel
relations: [{
type: Backbone.HasMany
key: 'subcategories'
relatedModel: 'App.Models.Subcategory'
collectionType: 'App.Collections.Subcategories'
reverseRelation: {
key: 'category',
includeInJSON: 'id'
}
}]
App.Models.Category.setup() # Set up BB Relational
Subcategory.js.coffee:
class App.Models.Subcategory extends Backbone.RelationalModel
relations: [{
type: Backbone.HasMany
key: 'items'
relatedModel: 'App.Models.Item'
collectionType: 'App.Collections.Items'
reverseRelation: {
key: 'subcategory',
includeInJSON: 'id'
}
}]
App.Models.Subcategory.setup() # Set up BB Relational
Item.js.coffee
class App.Models.Item extends Backbone.RelationalModel
initialize: ->
...
App.Models.Item.setup() # Set up BB Relational
Problem:
Calling item.get('subcategory') works as expected, returning a Backbone RelationalModel object. However, for some reason calling category returns a generic JS object.
item.get('subcategory').get('category')
Returns: Object {id: 1, title: "the title"}
In case it's related, console.log @subcategory.relations shows the message "collectionKey=subcategory already exists on collection=true ".