Model classes in BackboneJS

backbonJS is based on MV* patterns and designed for developing single page applications. It provides some basic and essential functionalities like attributes, events and validations for models. A model of backboneJS encapsulates model data that is further available for an application to interact with the user interface.

"Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface. With Backbone, you represent your data as Models, which can be created, validated, destroyed, and saved to the server."

As I'm only talking about backbonJS model classes in this blog post, so take a look at a model class as example:

  1. var Info= Backbone.Model.extend({
  2.    defaults: {
  3.         'name': 'Ehsan',
  4.         'address': {
  5.             'street': '14st Street'
  6.             'city': 'Tehran',
  7.             'Phone': +9809193782914
  8.         }
  9.    }
  10. });

The most important part of the above object is Backbone.Model.extend which is a backboneJS Operand. you can also create an object from the above Model:

  1. var infoObj= new Info({
  2.     'name': 'Ben',
  3.     'phone' '+79***********'
  4. });

by creating object, you will get a reference to that object, and any manipulation to the object will directly manipulate the actual object in the model. For example you can manipulate the above model  like this:

  1. Info.set('name', 'Jim.', { validate: true });

Tags: JS Libraries

comments powered by Disqus