Blog

Filter posts by Category Or Tag of the Blog section!

Model classes in BackboneJS

Friday, 21 March 2014

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 an example:

 

var Info= Backbone.Model.extend({

   defaults: {

        'name': 'Ehsan',

        'address': {

            'street': '14st Street'

            'city': 'Tehran',

            'Phone': +9809193782914

        }

   }

});

 

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:


 
var infoObj= new Info({

    'name': 'Ben',

    'phone' '+79***********'

});

 

by creating an 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:

 

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

 

comments powered by Disqus