Backbone Model

by gianlucaguarini

HTML

<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<ul id="result"></ul>

JavaScript

// this example show how to create a backbone model
var Model= Backbone.Model.extend();
// creating an instance that point to our model prototype and setting some attributes
var myModel = new Model({
    name : 'foo',
    id: 1234
});
// you can override or add properties to your models using set method
myModel.set({
    surname: 'bar'
});
// convert our model into a JSON file
var modeltoJSON = JSON.stringify( myModel );

// parsing json obtaining every attribute of our model
JSON.parse( modeltoJSON , function ( key , value ) {
    // printing the result just if it is a number or a string
    if( ( typeof value === "string" ) || ( typeof value === "number" )) {    
        $('#result').append( '<li>' + key + ' : ' + value + '</li>');
    }
})