Models.Base = Backbone.Model.extend({ classType: "MODELS.BASE", value: "", //String initialValue: "", //String initialize: function() { //this.value = this.get("value"); this.set("initialValue", this.get("value")); this.postInitialize(); }, postInitialize: function() {}, getJSONObject : function () { var toReturn = { value: this.get("value") }; if (this.initialValue) { toReturn.initialValue = this.get("initialValue"); } return toReturn; }, clear: function() { this.set("value", ""); this.set("label", ""); }, isEmpty: function() { return this.get("value") == ""; }, getAdditionalJSONObject: function() {} }); Models.BaseCollection = Backbone.Collection.extend({ classType: "MODELS.BASECOLLECTION", model: Models.Base, initialize: function() { this.postInitialize(); }, postInitialize: function() { }, getByProperty: function(sPropertyName, sValue) { var toReturn = null; this.each(function(oValueModel) { if (oValueModel.get(sPropertyName) == sValue) { toReturn = oValueModel; } }); return toReturn; }, sortByProperty: function(sPropertyName) { this.comparator = function(item) { return item.get(sPropertyName); }; this.sort(); }, clear: function() { this.reset(); }, //Return true if all Model in the collection are empty isEmpty: function() { var result = this.length==0; if (!result) { var allModelEmpty = true; this.each(function(oValueModel) { if (allModelEmpty) { allModelEmpty = oValueModel.isEmpty(); } }); result = allModelEmpty; } return result; }, setValue: function(oModelValue) { if (!oModelValue) { oModelValue = new this.model(); } this.clear(); this.add(oModelValue); }, addValue: function(oModelValue) { if (!oModelValue) { oModelValue = new this.model(); } this.add(oModelValue); }, addValueAt: function(oModelValue, index) { if (!oModelValue) { oModelValue = new this.model(); } this.add(oModelValue, {at: index}); }, //Clone a Collection //The result is a new typed collection even if the baseCollection is overriden by another collection //IE : If you call .clone() on a Models.PersonCollection, the cloned collection will be a Models.PersonCollection. No need to redefine .clone() //in Models.PersonCollection as Models.BaseCollection().clone() returns typed collections clone: function() { var newCollection = new this.constructor(); this.each(function(aModel) { newCollection.add(aModel.clone()); }); return newCollection; }, getJSONObject : function () { var toReturn = {}; var additionalPropertiesFromModel = this.getAdditionalJSONObject(); if (additionalPropertiesFromModel) { for (var attrname in additionalPropertiesFromModel) { toReturn[attrname] = additionalPropertiesFromModel[attrname]; } } return toReturn; }, getAdditionalJSONObject: function() {} });