将Backbone集合与嵌套对象一起使用

时间:2017-03-18 00:41:48

标签: javascript json backbone.js

我有一个用Backbone.Collection包装的JSON,如此 -

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

更新任何嵌套数据时,不会触发集合上的“更改”事件。通常,您会期望在阵列中包含对象的平面结构。但是考虑到这种嵌套结构,我应该如何设置这个集合。最终目标是拥有一个知道更新的集合(添加,编辑,删除)。

0 个答案:

没有答案