13

我正在学习Backbone。

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

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

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

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

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

4

4 回答 4

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
});
于 2013-01-10T17:47:24.497 回答
17

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

基本上,在这种情况下,您将创建不同的模型,并添加一个决胜局属性,例如“类型”。

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'}
]);
于 2014-07-27T07:45:12.640 回答
1

在这种情况下,骨干文档不完整。merge:true与option 和.一起使用时将不起作用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
});
于 2016-02-24T13:32:45.060 回答
-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();
            },
于 2017-03-28T11:35:02.203 回答