Backbone.js: Model Cheat Sheet

MethodsSyntax & Description
extendBackbone.Model.extend(properties, [classProperties])To create a Model class of your own, you extend Backbone.Model and provide instance properties, as well as optional classProperties to be attached directly to the constructor function.
preinitializenew Model([attributes], [options])For use with models as ES classes. If you define a preinitialize method, it will be invoked when the Model is first created, before any instantiation logic is run for the Model.
constructor / initializenew Model([attributes], [options])When a model instance is created, the class’s constructor gets called and it is invoked by defining the initialize function when the model is created.
getmodel.get(attribute)Get the current value of an attribute from the model. 
setmodel.set(attributes, [options])Set a hash of attributes (one or many) on the model. 
escapemodel.escape(attribute)It is like the get function, but returns the HTML-escaped version of a model’s attribute.
hasmodel.has(attribute)Returns true
 if the attribute is set to a non-null or non-undefined value.
unsetmodel.unset(attribute, [options])Remove an attribute by deleting it from the internal attributes hash. Fires a “change”
 event unless silent
 is passed as an option.
clearmodel.clear([options])Removes all attributes from the model, including the id
 attribute. Fires a “change”
 event unless silent
 is passed as an option.
idmodel.idA special property of models, the id is an arbitrary string (integer id or UUID). If you set the id in the attributes hash, it will be copied onto the model as a direct property.
idAttributemodel.idAttributeDefines a model’s unique identifier which contains the name of the member of the class which will be used as id.
cidmodel.cidA special property of models, the cid or client id is a unique identifier automatically assigned to all models when they’re first created.
attributesmodel.attributesAttributes defines property of a model.
changedmodel.changedChanges all the attributes that have changed after setting the attributes using the set() method.
defaultsmodel.defaults or model.defaults()The defaults hash (or function) can be used to specify the default attributes for your model.
toJSONmodel.toJSON([options])Returns a copy of the attributes as an object for JSON stringification.
syncmodel.sync(method, model, [options])Uses Backbone.sync to persist the state of a model to the server. Can be overridden for custom behavior.
fetchmodel.fetch([options])Merges the model’s state with attributes fetched from the server by delegating to Backbone.sync
savemodel.save([attributes], [options])Saves the data of the model by delegating to sync() method which reads and saves the model every time when a Backbone calls it.
destroymodel.destroy([options])Destroys or removes the model from the server by using the Backbone.sync method which delegates the HTTP “delete” request.
validatemodel.validate(attributes, options)If the input is invalid, it returns a specified error message or if the input is valid, it doesn’t specify anything and simply displays the result.
validationErrormodel.validationErrorThe value returned by validate during the last failed validation.
isValidmodel.isValid(options)Run validate to check the model state.
urlmodel.url()Returns the relative URL where the model’s resource would be located on the server.
urlRootmodel.urlRoot or model.urlRoot()Enables the url function by using the model id to generate the URL.
parsemodel.parse(response, options)Returns the model’s data by passing through the response object and represents the data in the JSON format.
clonemodel.clone()Returns a new instance of the model with identical attributes.
isNewmodel.isNew()Has this model been saved to the server yet? If the model does not yet have an id, it is considered to be new.
hasChangedmodel.hasChanged([attribute])Has the model changed since its last set? If an attribute is passed, returns true
 if that specific attribute has changed.
changedAttributesmodel.changedAttributes([attributes])It returns the model’s attributes that have changed since the last set or else becomes false, if there are no attributes.
previousmodel.previous(attribute)During a “change”
 event, this method can be used to get the previous value of a changed attribute.
previousAttributesmodel.previousAttributes()Return a copy of the model’s previous attributes. Useful for getting a diff between versions of a model, or getting back to a valid state after an error occurs.

Leave a Reply

Your email address will not be published. Required fields are marked *