The Backbone.JS Parse() model is used to get the model’s data (in the JSON format) by passing through the response object.
Syntax:
Model.Parse (response, options)
Parameters:
response: This parameter is passed using response raw object in order to get the attributes to be set on the model.
options: This parameter is used to specify the options like id, name etc. for a model.
Example:
<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 ={
"Messages": {
"msg1": "Hello",
"msg2": "Hi",
"msg3": "Hey"
} };
var Y = Backbone.Model.extend({
parse : function(response, options){
document.write(JSON.stringify(response));
} });
var Z = new Y(X, {parse: true});
</script>{"Messages":{"msg1":"Hello","msg2":"Hi","msg3":"Hey"}}
<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 ={
"Messages": {
"msg1": "Hello",
"msg2": "Hi",
"msg3": "Hey"
} };
var Y = Backbone.Model.extend({
parse : function(response, options){
document.write(JSON.stringify(response));
} });
var Z = new Y(X, {parse: true});
</script>{"Messages":{"msg1":"Hello","msg2":"Hi","msg3":"Hey"}}
Example {"Messages":{"msg1":"Hello","msg2":"Hi","msg3":"Hey"}}
Output:
{"Messages":{"msg1":"Hello","msg2":"Hi","msg3":"Hey"}}
{"Messages":{"msg1":"Hello","msg2":"Hi","msg3":"Hey"}}
{"Messages":{"msg1":"Hello","msg2":"Hi","msg3":"Hey"}}
Explanation:
In the above example, the output is the model’s data in the JSON format.