下面是我已经手动构建的集合的当前代码结构。我在我的服务器上有一个json文件,我现在正在尝试加载,基本上删除手动文件并基于该数据构建一个集合。想知道我可能需要在下面更改我的代码以适应此问题。从外部JSON骨干填充集合
var Game = Backbone.Model.extend({
defaults: {
name: 'John Doe',
age: 30,
occupation: 'worker'
}
});
var GameCollection = Backbone.Collection.extend({
model: Game,
url: 'path/to/json',
parse: function(response) {
return response;
}
});
var GamesView = Backbone.View.extend({
tagName: 'ul',
render: function() {
//filter through all items in a collection
this.collection.each(function(game){
var gameView = new GameView({model: game});
this.$el.append(gameView.render().el);
}, this)
return this;
}
});
var GameView = Backbone.View.extend({
tagName: 'li',
template: _.template($('#gameTemplate').html()),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
var gameCollection = new GameCollection([
{
name: 'John Doe',
age: 30,
occupation: 'worker'
},
{
name: 'John Doe',
age: 30,
occupation: 'worker'
},
{
name: 'John Doe',
age: 30,
occupation: 'worker'
}
]);
var gamesView = new GamesView({collection: gameCollection});
$(document.body).append(gamesView.render().el);
所以你现在说'新的GameCollection(some_big_array_of_stuff)',但现在你想从服务器加载集合? –
是的,我不想使用我为测试创建的手动对象数组,但我现在想尝试加载服务器上的json对象并尝试解析该数据。不知道如何去做。 – Anks