简体   繁体   English

Backbone Collection有多个型号?

[英]Backbone Collection with multiple models?

I'm learning Backbone. 我正在学习Backbone。

I want to create a list that can contain different models, with different attributes. 我想创建一个包含不同模型的列表,具有不同的属性。

For example, listing folder contents, which could include models of type file and models of type folder, in any order. 例如,按任何顺序列出文件夹内容,其中可包括类型文件的模型和类型文件夹的模型。

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

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

What is the proper way to represent this in Backbone? 在Backbone中表示这个的正确方法是什么? Is it possible to have a single Collection with multiple models? 是否可以使用多个模型的单个集合?

Create a base model that your other models inherit from: 创建其他模型继承自的基本模型:

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
});

And make the model type of the collection be that same base model: 并使集合的model类型与基本模型相同:

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

You could also do it the backbone way. 你也可以做骨干方式。 Check out the docs backbone collection 查看docs backbone集合

Basically you would create different models adding a tie breaker attribute say "type" in this case. 基本上你会创建不同的模型,在这种情况下添加一个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);
            case 'folder':
                return new folder(model, options);
        }
    }
});

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

Backbone documention is not complete in this case. 在这种情况下,主干文档不完整。 It will not work when used with merge:true option and idAttribute . merge:true选项和idAttribute一起使用时, idAttribute In that case you need to: 在这种情况下,您需要:

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
});
        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();
            },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM