Tuesday, 12 June 2018

012 BackboneJS Model urlRoot,parse,clone,isNew,hasChanged,changedAttributes,previous,previousAttributes

urlRoot : You can use model outside of a collection , then return the url to where the model's resource is located.

Syntax : model.urlRoot or model.urlRoot()

Example

<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">


var student = Backbone.Model.extend({
urlRoot: '/tutorialspoint/backbonejs'
});

var stuObj = new student();

document.write(stuObj.url());

</script>
</head>
</html>


Output :



/tutorialspoint/backbonejs 


parse : Method returns the model data by passing through the response object and represents the data in JSON format.

Syntax : model.parse(response, options)

Example


<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">


var myData = {
"values": [{
"name": "john",
"age": "12"
}]
};

var student = Backbone.Model.extend({
parse: function (response, options) {
document.write(JSON.stringify(response));
}
});

var stuObj = new student(myData, { parse: true });
</script>
</head>
</html>

Output :

{"values":[{"name":"john","age":"12"}]} 



clone :Returns a new instance of the model just coping one model object to another object. 

Syntax : model.clone()

Example

<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">

          var student = Backbone.Model.extend();

           var stuObj = new student({name:'John',age:12});

           var stuObj2 = stuObj.clone();


            document.write(JSON.stringify(stuObj2));
</script>
</head>
</html>


Output :


{"name":"John","age":12} 


isNew : Detect model been saved to the server of not.
 
Syntax : model.isNew()

Example

<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">

          var student = Backbone.Model.extend();
          var stuObj = new student();

          var stuObj1 = new student({ name: 'john', age: 15 });


          document.write(stuObj.isNew());


           document.write("");

          document.write(stuObj1.isNew());

</script>
</head>
</html>

Output :

true
true


hasChanged : The model has changed since its last set.If that specific attribute has changed. 

Syntax :model.hasChanged([attribute])


Example


<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">

var student = Backbone.Model.extend();
var stuObj = new student({ name: 'john', age: 15 });


document.write(stuObj.hasChanged());

stuObj.set({ 'name': 'john 2' });

document.write("");
document.write(stuObj.hasChanged());


</script>
</head>
</html>

Output :

false
true 


changedAttributes : changedAttributes method returns the hash of model' s attributes that have been changed since the last set.It return false , if there is no attributes.

Syntax : model.changedAttributes([attributes])

Example

<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">

var student = new Backbone.Model({
name1: 'john',
age: 12,
});
student.on('change', function () {
document.write(JSON.stringify(student.changedAttributes()));
});
student.set({
name1: 'john2',
age: 15
});
</script>
</head>
</html>


Output :

{"name1":"john2","age":15}



previous :
It retrive the previous value of the attribute.

Syntax : model.previous(attribute).
Example

<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">

var student = new Backbone.Model({
name: 'john',
age: 12
});

document.write("Name is :" + student.get('name'));

document.write('');
student.set({
name: 'john2',
age: 15
});

document.write("After change , Name is :" + student.get('name'));

document.write('');

document.write("Here is previous value is :" + student.previous("name"));

</script>
</head>
</html>


Output :

Name is :john
After change , Name is :john2
Here is previous value is :john


previousAttributes :The Backbone.js PreviousAttributes gives the model's previous attributes before the last change event.

Syntax : model.previousAttributes() 
 

Example 

<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">

var student = new Backbone.Model({
name: 'john',
age: 12
});

document.write("Name is :" + student.get('name'));

document.write('');
student.set({
name: 'john2',
age: 15
});

document.write('');
document.write(JSON.stringify(student.previousAttributes()));
</script>
</head>
</html>


Output :

 Name is :john

{"name":"john","age":12}
























 

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...