Backbone.js +关系型+ AMD ...引导关系
我感觉有点迷失并忽略了一些东西,但我不知道如何解决这个问题,甚至不太确定如何询问...
首先,我使用 AMD 方法(带有curl.js 库),这使得这可能更困难,但我不会因为这个问题而放弃AMD。
我有来自服务器的引导数据结构,存储在“window.bootstrap”属性中。
Departments = [
{"Id": 1, "Name": "Early Collections" },
{"Id": 2, "Name": "Collections" }
]
Blocks = [
{"Id": 1, "Code": "K", "Department": 1 },
{"Id": 2, "Code": "A", "Department": 2 }
]
现在我对解决这个问题的方法感到困惑。这是我的“DataModel/Block”模块:
define [
'Collection/DepartmentCollection'
'DataModel/Department'
], (DepartmentCollection, Department) ->
Backbone.RelationalModel.extend
relations: [
type: Backbone.HasOne
key: 'Department'
relatedModel: Department
collectionType: DepartmentCollection
]
模块“DataModel/Department”只是普通的 RelationalModel,没有任何关系。另外,这里提到的每个集合也是简单的,除了对模型的引用之外没有任何东西,如下所示:
define ['DataModel/Department'] , (Department) ->
Backbone.Collection.extend
model: Department
最后,这里是 Bootstrap 模块,它看起来像这样:
define [
'DataModel/Department'
'Collection/DepartmentCollection'
'DataModel/Block'
'Collection/BlockCollection'
] , (Department, DepartmentCollection, Block, BlockCollection) ->
model = Backbone.RelationalModel.extend
relations: [
type: Backbone.HasMany
key: 'Departments'
relatedModel: Department
collectionType: DepartmentCollection
,
type: Backbone.HasMany
key: 'Blocks'
relatedModel: Block
collectionType: BlockCollection
]
data = window.bootstrap || {}
boot = new model
boot.get('Departments').reset data.Departments || []
boot.get('Blocks').reset data.Blocks || []
return boot
我希望从中找到这些块的部门并在那里分配模型,但是打电话
console.debug ins.get('Blocks').at(0).get('Department')
...让我不确定。
但这还没有结束。我也将拥有来自服务器的与部门相关的其他实体。我想看到,它会自动从引导程序附加部门,这样我就可以透明地使用它。
我不知道我是否误解了这个关系库,或者它还没有准备好。任何帮助表示赞赏。
I feel sort of lost and overlooking something, but i am not sure how to approach to this and even not very much sure how to ask...
First of all, i am using AMD approach (with curl.js library), which makes this probably more difficult, but i am not giving up on AMD because of this problem.
I have this structure of bootstrap data from the server, stored in 'window.bootstrap' property.
Departments = [
{"Id": 1, "Name": "Early Collections" },
{"Id": 2, "Name": "Collections" }
]
Blocks = [
{"Id": 1, "Code": "K", "Department": 1 },
{"Id": 2, "Code": "A", "Department": 2 }
]
Now i am confused about approach to this. Here is my 'DataModel/Block' module:
define [
'Collection/DepartmentCollection'
'DataModel/Department'
], (DepartmentCollection, Department) ->
Backbone.RelationalModel.extend
relations: [
type: Backbone.HasOne
key: 'Department'
relatedModel: Department
collectionType: DepartmentCollection
]
Module 'DataModel/Department' is just plain RelationalModel without any relations. Also every mentioned Collection here is also plain without anything but reference to Model like this:
define ['DataModel/Department'] , (Department) ->
Backbone.Collection.extend
model: Department
And finally, here goes Bootstrap module, which looks like this:
define [
'DataModel/Department'
'Collection/DepartmentCollection'
'DataModel/Block'
'Collection/BlockCollection'
] , (Department, DepartmentCollection, Block, BlockCollection) ->
model = Backbone.RelationalModel.extend
relations: [
type: Backbone.HasMany
key: 'Departments'
relatedModel: Department
collectionType: DepartmentCollection
,
type: Backbone.HasMany
key: 'Blocks'
relatedModel: Block
collectionType: BlockCollection
]
data = window.bootstrap || {}
boot = new model
boot.get('Departments').reset data.Departments || []
boot.get('Blocks').reset data.Blocks || []
return boot
I would expect from this, that it would find Departments for those Blocks and assign models there, but calling
console.debug ins.get('Blocks').at(0).get('Department')
...gets me undefined.
But this is not the end. I will be having other entities from server with relation to Department too. And i would like to see, it automatically attaches Department from that bootstrap, so i can use it transparently.
I don't know if i had just misunderstood this relational library, or it's not AMD ready. Any help is appreciated.
潜在的范围/名称解析问题?
console.debug(window.Block, window.Department)
得到什么输出?如果您确实获取了模型类型,则以字符串形式提供latedModel
可能会有所帮助,例如latedModel: "Department"
。Potential scoping / name resolution problem? What output do you get for
console.debug(window.Block, window.Department)
? If you do get the model type, it might help to give therelatedModel
as a string, e.g.relatedModel: "Department"
.