我有一个网址http://anexampleproject/api/players
以json格式返回播放器列表。
如何在控制台中创建模型及其集合并提醒名称。
url返回json的例子:
[
{
"id": 1,
"name": "Lily",
"age": 14,
"city": New York,
},
{
"id": 2,
"name": "BIlly",
"age": 14,
"city": New York,
}
]
答案 0 :(得分:8)
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);
编辑:
使用网址
var MyCollection = Backbone.Collection.extend({
url: "http://anexampleproject/api/players",
model: MyModel
});
var myCollection = new MyCollection();
myCollection.fetch({
success: function(){
},
error: function(){
}
});