Friday, 11 December 2015

Exercise-8: Model

Introduction


Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control.

Solution

Include following code in 'main.js' and check conole.


var human = Backbone.Model.extend({
            initialize: function(){
                console.log("Model got initialized);
});
var person = new human; 

Using default value to set Model Attributes

Modify above code with following and check console.

var human = Backbone.Model.extend({

defaults: {
name: 'Guest User',
age: 23,
occupation: 'Worker'
},
initialize: function(){
                console.log("Model got initialized);
});

var person new human; 
console.log(person.get('name'))


Using constructor to set Model Attributes


var human = new Person({ name: "Twomot", age: 01}); 
console.log(person.get('name')+'-'+console.log('age'));


Using getters / Setters

var human new Person({ name: "Wipro"}); 
human.set('age', 23);


console.log(person.get('name')+'-'+console.log('age'));



No comments:

Post a Comment