The main collection with several models?

I am learning Backbone.

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

For example, listing the contents of a folder, which may include type file models and type folder models, in any order.

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

What is the right way to present this on a Backbone network? Is it possible to have one collection with several models?

+13
javascript backbone.js-collections backbone-collections
Jan 10 '13 at 17:29
source share
4 answers

Create a base model that your other models will inherit:

 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 create a model type for the collection of the same base model:

 var DataCollection = Backbone.Collection.extend({ model: DataModel }); 
+26
Jan 10 '13 at
source share

You can also do this in the main way. Check out the database of documents

In fact, you would create different models, adding in this case the link breaker attribute, say, “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'} ]); 
+17
Jul 27 '14 at 7:45
source share

The backbone documention in this case is not complete. It will not work when using the merge:true and idAttribute . In this 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 }); 
+1
Feb 24 '16 at 13:32
source share
  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(); }, 
-2
Mar 28 '17 at 11:35
source share



All Articles