CodesJava

Easy learning with example program codes

Backbone.JS Parse() collection


The Backbone.JS Parse() collection method is used to get the collection’s data by passing through the response object.

Syntax:

Collection.Parse ( response, options )   

Parameters:
response: This parameter is used to specify the array of model attributes.
options: This parameter is used to specify whether to represent the data in JSON format or not.

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>  
</head>  
<body>  
<script type="text/javascript">    
var X = Backbone.Model.extend();  
var data ={  
"names": [{  
"first": "Jimmy",  
"second": "Tommy",  
"third": "Bruno"  
}]  };  
var Y = Backbone.Collection.extend({  
model: X,   
parse : function(response, options){  
document.write(JSON.stringify(response));  
}  
});  
var Z = new Y(data, { parse: true });  
</script>  
</body>  
</html>

Output:

{"names":[{"first":"Jimmy","second":"Tommy","third":"Bruno"}]}

Explanation:
In the above example, the Parse() method is used to get the value of the ‘data’ extracted by the collection Y when parse is set as “true”.

Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki