javascript 具有多种型号的 Backbone Collection?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14263765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:10:28  来源:igfitidea点击:

Backbone Collection with multiple models?

javascriptbackbone.jsbackbone.js-collections

提问by user1031947

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? Is it possible to have a single Collection with multiple models?

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

回答by Lukas

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 modeltype of the collection be that same base model:

并使model集合的类型与基本模型相同:

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

回答by Geofrey Ssekirime

You could also do it the backbone way. Check out the docs backbone collection

你也可以用骨干的方式来做。查看文档主干集合

Basically you would create different models adding a tie breaker attribute say "type" in this case.

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

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

回答by Maciej Dzikowicki

Backbone documentionis not complete in this case. It will not work when used with merge:trueoption and idAttribute. In that case you need to:

在这种情况下,主干文档并不完整。与merge:trueoption 和 一起使用时它将不起作用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
});

回答by zloctb

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