1

Hello I have a model in backbone that looks like this,

    Project {cid: "c2", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
    _changing: false
    _pending: false
    _previousAttributes: Object
    attributes: Object
        brief: ""
        colour: "#2a5563"
        created: "2014-04-22 16:15:57"
        created_by: "Me"
        creator_id: "14"
        dates: Array[1]
        files: Array[8]
        items: Array[7]
        progress: "0"
        project_id: "7692"
        project_name: "Rendering on new task"
        status: "1"
        tasks: Array[1]
    __proto__: Object
    changed: Object
    cid: "c2"
    __proto__: ctor

The items array needs to a model, the items array is currently made of multiple of objects, and each object has an attribute called subitems - this attribute is a array, but I think it needs to be a collection, in fact every think other than the item array needs to be collection.

What is the best way to do this?

Udders
  • 6,914
  • 24
  • 102
  • 194

1 Answers1

1

You can make items a Backbone collection having a set of items. use

var itemsArray = project.get('items');
itemsArray.each(function (item) {
    item = new Item();//collection/model.
});
var items = new Items(itemsArray);//Items is a backbone collection.
project.set('items', items);

and can access it as

project.get('items');

This way you can nest any number of collections/models.

sap
  • 343
  • 3
  • 11