對不起,如果這是一個超級noob問題,但我拼命地學習Backbone.js的工作,我花了整整一週閱讀和觀看codeschool.com上的視頻和我仍然無法弄清楚這一點。找不到backbone.js
我想呈現一個json文件作爲模板中的表,但我無法弄清楚我做錯了什麼,每次我嘗試一些我做得越來越差的事情。
這裏是我的JS:
Game = Backbone.Model.extend({});
Games = Backbone.Collection.extend({
model: Game
})
window.AllGames = new Games();
AllGames.fetch({
url: 'games_in_progress.json'
, success: function() {
//console.log(AllGames.toJSON());
}
});
GameList = Backbone.View.extend({
el: 'body',
listTemplate: _.template($('#game_list').html(), {}),
initialize: function(){
//this.render();
this.collection = new AllGames();
this.collection.bind("reset", this.render, this);
this.collection.fetch();
},
render: function(){
//var listTemplate = _.template($('#game_list').html(), {});
//this.$el.html(listTemplate);
console.log(this.collection.toJSON());
$(this.el).html(this.listTemplate(this.collection.toJSON()));
}
});
var gameList = new GameList({
el:$(".content")
});
這裏是我試圖導入JSON文件:
{
"title": "Tic Tac Toe Games in Progress",
"games": [
{"id": 1, "player1": "Eric Morgan", "player2": "Charles May", "player1_wins": 0, "player2_wins": 2},
{"id": 2, "player1": "Valerie Bowman", "player2": "Felicia Cole", "player1_wins": 0, "player2_wins": 0},
{"id": 3, "player1": "Guy Benson", "player2": "Norman Day", "player1_wins": 3, "player2_wins": 3},
{"id": 4, "player1": "Emanuel Weaver", "player2": "Sammy Nelson", "player1_wins": 10, "player2_wins": 4},
{"id": 5, "player1": "Oliver Mcbride", "player2": "Jill Hansen", "player1_wins": 102, "player2_wins": 30},
{"id": 6, "player1": "Alison Bell", "player2": "Owen Dixon", "player1_wins": 10, "player2_wins": 200},
{"id": 7, "player1": "Lawrence Adams", "player2": "Ebony Schmidt", "player1_wins": 1, "player2_wins": 0},
{"id": 8, "player1": "Tony Robbins", "player2": "Shelley Mills", "player1_wins": 5, "player2_wins": 21},
{"id": 9, "player1": "Dustin Watts", "player2": "Glen Clayton", "player1_wins": 12, "player2_wins": 19},
{"id": 10, "player1": "Douglas Bates", "player2": "Eduardo Martin", "player1_wins": 1, "player2_wins": 3},
{"id": 11, "player1": "Lola Swanson", "player2": "Nichole Carlson", "player1_wins": 101, "player2_wins": 0},
{"id": 12, "player1": "Fernando Mitchell", "player2": "Bessie Sharp", "player1_wins": 9, "player2_wins": 0},
{"id": 13, "player1": "Randal Austin", "player2": "Rodolfo Mccormick", "player1_wins": 7, "player2_wins": 90},
{"id": 14, "player1": "Louise Chandler", "player2": "Laura Beck", "player1_wins": 93, "player2_wins": 112},
{"id": 15, "player1": "Ernesto Brock", "player2": "Lois Singleton", "player1_wins": 109, "player2_wins": 87},
{"id": 16, "player1": "Lora Osborne", "player2": "Katie Johnston", "player1_wins": 201, "player2_wins": 7},
{"id": 17, "player1": "Priscilla Horton", "player2": "Julius Hale", "player1_wins": 10, "player2_wins": 53},
{"id": 18, "player1": "Andrew Fernandez", "player2": "Henrietta Morris", "player1_wins": 30, "player2_wins": 99},
{"id": 19, "player1": "Joe Shelton", "player2": "Martin Ramsey", "player1_wins": 34, "player2_wins": 20},
{"id": 20, "player1": "Mary Little", "player2": "Pam Goodwin", "player1_wins": 29, "player2_wins": 43},
{"id": 21, "player1": "Lynda Nguyen", "player2": "Jermaine Hughes", "player1_wins": 61, "player2_wins": 71},
{"id": 22, "player1": "Clifford Young", "player2": "Tonya Baldwin", "player1_wins": 0, "player2_wins": 0},
{"id": 23, "player1": "Adrienne Kennedy", "player2": "Jordan Howard", "player1_wins": 10, "player2_wins": 1},
{"id": 24, "player1": "Lena Newton", "player2": "Kristi Guerrero", "player1_wins": 30, "player2_wins": 1},
{"id": 25, "player1": "Alicia Casey", "player2": "Jody Lopez", "player1_wins": 0, "player2_wins": 0}
]
}
我知道這是不好的,但其實我喜歡骨幹,我真的想學習它。如果任何人都可以幫助,我將不勝感激。
此外,如果有推薦的書籍可供閱讀,我也會很感激。我目前正在閱讀Thomas Davis的「Backbone Tutorials」(Backbone Tutorials)(https://leanpub.com/backbonetutorials),但如果有比這更好的資源或者codeschool.com,我將不勝感激。
已經更新了答案 –
感謝您的。你有什麼資源可以推薦我閱讀,因爲我完全迷失了。你是如何學習Backbone的? –