我正在尝试使用Backbone.Relational在我的应用程序中设置一些关联。
基本上我有 BackboneSearch
和Service
模型。搜索有一个ServiceList
包含许多服务的集合。
但是,我似乎无法从服务初始化程序中访问父搜索。当我尝试记录父搜索时,我得到null
. 谁能看到我做错了什么?
我的搜索模型是这样设置的(代码可能有语法错误,我正在从 coffeescript 即时翻译):
var Search = new Backbone.RelationalModel({
urlRoot: '/searches',
relations: [{
type: Backbone.HasMany,
key: 'services',
relatedModel: 'Service',
collectionType: 'ServiceList',
reverseRelation: {
key: 'search'
}
}],
initialize: function(options) {
// => Search "has services:", ServiceList
console.log this, "has services:", @get('services');
}
});
var Service = new Backbone.RelationalModel
initialize: function() {
// => Service "in" null
console.log this, "in", @get('search');
}
});
或者,如果您更喜欢 CoffeeScript:
class Search extends Backbone.RelationalModel
urlRoot: '/searches'
relations: [
type: Backbone.HasMany
key: 'services'
relatedModel: 'Service'
collectionType: 'ServiceList'
reverseRelation:
key: 'search'
]
initialize: (options) ->
// => Search "has services:", ServiceList
console.log this, "has services:", @get('services')
class Service extends Backbone.RelationalModel
initialize: ->
// => Service "in" null
console.log this, "in", @get('search')