CodesJava

Easy learning with example program codes

Backbone.JS toJSON()

The Backbone.JS toJSON() model is used to get a copy of the attributes as an object for JSON stringification.

Syntax:

Model.toJSON (options)

Parameters:
options: This parameter is used to specify options like the variable name and id.

Example:

<!DOCTYPE html>
<html>
<head>  
<title>Example</title>  
<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  X = Backbone.Model.extend();  
var Y = new X({  
subject: "Gratitude",  
msg: "Thanks for everything."  
});  
document.write(JSON.stringify(Y));  
</script> 
</head>
</html>

Output:

{"subject":"Gratitude","msg":"Thanks for everything."}

Explanation:
A copy of the attributes is returned as an object for JSON stringification.

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status