admin管理员组文章数量:1391987
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,
}
]
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,
}
]
Share
Improve this question
edited Mar 14, 2013 at 9:02
Backlin
14.9k4 gold badges52 silver badges81 bronze badges
asked Mar 14, 2013 at 8:57
LasangLasang
1,3796 gold badges24 silver badges44 bronze badges
1 Answer
Reset to default 8var 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(){
}
});
本文标签: javascriptfetching data from server with backbone and creating model and collectionStack Overflow
版权声明:本文标题:javascript - fetching data from server with backbone and creating model and collection - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744697682a2620379.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论