Backbone Collection有多个型号?

时间:2013-01-10 17:29:22

标签: javascript backbone.js backbone-collections

我正在学习Backbone。

我想创建一个包含不同模型且具有不同属性的列表。

例如,列出文件夹内容,可以包括任何顺序的类型文件模型和类型文件夹模型。

file : {
  title : "",
  date : "",
  type : "",
  yaddayadda : ""
}

folder : {
  title : "",
  date : "",
  haminahamina : ""
}

在Backbone中表示这个的正确方法是什么?是否可以使用多个模型的单个集合?

4 个答案:

答案 0 :(得分:26)

创建其他模型继承的基本模型:

var DataModel = Backbone.Model.extend({
    // Whatever you want in here
});

var FileModel = DataModel.extend({
    // Whatever you want in here
});

var FolderModel = DataModel.extend({
    // Whatever you want in here
});

使集合的model类型成为相同的基本模型:

var DataCollection = Backbone.Collection.extend({
    model: DataModel
});

答案 1 :(得分:17)

你也可以做骨干方式。查看文档backbone collection

基本上你会创建不同的模型,在这种情况下添加一个tie breaker属性,例如“type”。

var file = Backbone.Model.extend({
        defaults: {
            // will need to include a tie breaker attribute in both models
            type: 'file'
        }
    }),
    folder = Backbone.Model.extend({
        defaults: {
            // tie breaker
            type: 'folder'
        }
    });

var fs = Backbone.Collection.extend({
    model: function(model, options) {
        switch(model.type) {
            case 'file':
                return new file(model, options);
                break;
            case 'folder':
                return new folder(model, options);
                break;
        }
    }
});

// after that just add models to the collection as always
new fs([
    {type: 'file',name: 'file.txt'},
    {type: 'folder',name: 'Documents'}
]);

答案 2 :(得分:1)

在这种情况下,

Backbone documention不完整。与merge:true选项和idAttribute一起使用时,它将无法使用。在这种情况下,您需要:

var ModelFactory = function (attr, options) {
  switch (attr.type) {
    case 'file':
      return new file(attr, options);
    case 'folder':
      return new folder(attr, options);
  }
};
ModelFactory.prototype.idAttribute = '_id';

var fs = Backbone.Model.extend({
   model: ModelFactory
});

答案 3 :(得分:-2)

        var bannedList = app.request('rest:getBan');
        var whiteIpList = app.request("rest:getWhite");
        var whiteGroupList = app.request("rest:....");
        $.when(bannedList, whiteIpList, whiteGroupList).
done(function (bannedList, whiteIpList, whiteGroupList) {
            var collection = new Backbone.Collection();
            collection.add(bannedList);
            collection.add(whiteIpList);
            collection.add(whiteGroupList);

        });


    app.reqres.setHandler("rest:getBannedList", function (data) {
        return API.getBannedList(data);
    });
    getBannedList: function (data) {
                var user = new Backbone.Model();
                user.url = '/banned';
                user.cid = 'bannedList';
                var defer = $.Deferred();

                user.fetch({
                    type: 'GET',
                    data: data,
                    success: function (data) {
                        defer.resolve(data);
                    },
                    error: function (data) {
                        defer.reject(data);
                    }
                });
                return defer.promise();
            },