0

I have a JSON which is wrapped with Backbone.Collection like so -

var data  = {
  "name": "File 1",
  "id": "Node100",
  "size": "large",
  "children": [
    {
      "name": "File 1-1",
      "id": "9da",
      "children": [
        {
          "name": "File 1-1-1",
          "id": "30a",
          "children": [],
          "type": "file_suspected",
          "link": {
            "id": "9d3",
            "type": "started_default"
          }
        }
      ],
      "type": "file_suspected",
      "link": {
        "id": "f70",
        "type": "started_default"
      }
    }
  ],
  "type": "file_default",
  "link": {
    "id": "970",
    "type": ""
  }
}

var collection = new Backbone.Collection(data);
collection.on('change', function(obj) {
  console.log("collection changed. ");
});
collection.at(0).set('type', 'hello');

//When nested data change, collections 'change' event does not get triggered.
collection.at(0).get('children')[0]['type'] = "hello_1";

When any of the nested data is updated, the 'change' event on the collection does not trigger. Typically, you would expect a flat structure with objects in an array. But given this nested structure, how should I go about setting up this collection. The end goal is to have a collection which is aware of updates (add, edit, remove).

Viswesh
  • 33
  • 4
  • You need to use plugins like backbone deep model, backbone relational or write such code by yourself. – T J Mar 18 '17 at 10:51
  • Also, check out [how to setup nested models and collections with Backbone](http://stackoverflow.com/a/40823148/1218980) and [how to bubble up nested models/collections events](http://stackoverflow.com/a/40532592/1218980) – Emile Bergeron Mar 20 '17 at 00:22
  • Thanks folks. I will asses suggested backbone plugins for my use case. – Viswesh Mar 21 '17 at 04:02

0 Answers0