骨干收集与多个模型?(Backbone Collection with multiple model

2019-07-18 02:16发布

我正在学习骨干。

我想创建一个可以包含不同型号,不同属性的列表。

例如,列出文件夹的内容,其中可能包括文件类型的模式和类型的文件夹模式,按任意顺序。

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

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

什么是骨干代表这个正确的方法是什么? 是否有可能有多个模型的单一集合?

Answer 1:

创建示范基地,你的其他车型继承:

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


Answer 2:

你也可以做到这一点的骨干道路。 退房的文档骨干集合

基本上你将创建不同的车型加入了决胜局属性说,在这种情况下,“类型”。

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


Answer 3:

骨干机制的文档是不是在这种情况下完成。 当用于它不会工作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
});


Answer 4:

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


文章来源: Backbone Collection with multiple models?