I have a url http://anexampleproject/api/players
which returns list of players in json format.
How can i create model and collection of it and alert name in console.
example of url returned json:
[
{
"id": 1,
"name": "Lily",
"age": 14,
"city": New York,
},
{
"id": 2,
"name": "BIlly",
"age": 14,
"city": New York,
}
]
var data = [{
"id": 1,
"name": "Lily",
"age": 14,
"city": "New York"
}, {
"id": 2,
"name": "BIlly",
"age": 14,
"city": "New York"
}];
var MyModel = Backbone.Model.extend({
defaults: {
"id": "",
"name": "",
"age": 0,
"city": ""
}
});
var MyCollection = Backbone.Collection.extend({
model: MyModel
});
var myCollection = new MyCollection(data);
EDIT:
Using url
var MyCollection = Backbone.Collection.extend({
url: "http://anexampleproject/api/players",
model: MyModel
});
var myCollection = new MyCollection();
myCollection.fetch({
success: function(){
},
error: function(){
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With