Backbone.js +关系型+ AMD ...引导关系

发布于 12-10 05:57 字数 2152 浏览 1 评论 0原文

我感觉有点迷失并忽略了一些东西,但我不知道如何解决这个问题,甚至不太确定如何询问...

首先,我使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一世旳自豪2024-12-17 05:57:54

潜在的范围/名称解析问题? 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 the relatedModel as a string, e.g. relatedModel: "Department".

心房的律动2024-12-17 05:57:54

看起来已经解决了问题出在一行代码中...

Backbone.Model.prototype.idAttribute = "Id"

我忘记了我正在使用 PascalCase 标识符作为对象属性。目前看来一切都很好。

Looks it's solved. Problem was in one line of code...

Backbone.Model.prototype.idAttribute = "Id"

I forgot i am using PascalCase identifiers for object properties. Everything looks ok for now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文