在现有集合中创建新集合

时间:2015-07-30 08:05:05

标签: javascript backbone.js

我目前正在尝试将骨干模型放入已有的模型中。我想知道这是否可能。

var rf = this.collection.create(attrs, options);
Model.set(table, rf);

由于

1 个答案:

答案 0 :(得分:1)

What you trying to do is "Nested Models & Collections". Backbone already has preferable approach. The common idea consist in storing of nested model directly in the instance of another model instead attributes.

So, you could create child model first and then pass it to parent model through options like the following:

var rf = this.collection.create(attrs, options);

var Model = Backbone.Model.extend({

  initialize: function(attributes, options) {
    _.isObject(options) || (options = {});

    if (options.child) {
        this.child = new options.child;
    }        
  }
});

var model = new Model({}, {child: rf});

If you want to get a fully supported tree-like Backbone models you could try to use one of the following plugins.

Hope this helps!

相关问题