I am trying to parse my json file into Backbone collection and model as like below.
This is .json file
[
{
"type": "rect",
"x": 10,
"y": 10,
"w": 100,
"h": 100,
"color": "red"
},
{
"type": "arc",
"x": 210,
"y": 20,
"w": 200,
"h": 150,
"color": "blue"
}
]
And I also have .html file as like below.
<script>
$(function() {
var JSONModel = Backbone.Model.extend({});
var MyCollection = Backbone.Collection.extend({
model : JSONModel,
url : 'profiles.json',
initialize : function() {
alert("init");
},
parse : function(response) {
for (var i = 0; i < response.length; i++) {
var tmpModel = new JSONModel();
tmpModel.set({
type : response[i].type,
x : response[i].x,
y : response[i].y,
w : response[i].w,
h : response[i].h,
color : response[i].color
});
this.add(tmpModel);
alert("inserting" + i);
}
return response;
}
});
var collection = new MyCollection();
collection.fetch();
alert(collection.length);
});
</script>
Q.
1.In this code, why fetch function call parse function?
2.Is there any other function which is called from fetch function?
3.Do you think how can I fix this code to get the json object? I cannot get the 'collection.length' in this code.
Please help.