Backbone Collection with multiple models?

前端 未结 4 1954
长发绾君心
长发绾君心 2020-12-13 21:52

I\'m learning Backbone.

I want to create a list that can contain different models, with different attributes.

For example, listing folder contents, which co

相关标签:
4条回答
  • 2020-12-13 22:06
            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();
                },
    
    0 讨论(0)
  • 2020-12-13 22:12

    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.

    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'}
    ]);
    
    0 讨论(0)
  • 2020-12-13 22:14

    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:

    var DataCollection = Backbone.Collection.extend({
        model: DataModel
    });
    
    0 讨论(0)
  • 2020-12-13 22:24

    Backbone documention is not complete in this case. It will not work when used with merge:true option and 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
    });
    
    0 讨论(0)
提交回复
热议问题