Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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".

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