這是我的代碼。隨時告訴我我做錯了什麼或者是對的。我試圖將所有內容存儲在PhishVids
對象中。如果這是做錯事的方法,請告訴我。在View中爲Backbone.js獲取JSON數據
我的JSON存儲在/shows/YEAR.json
。這些年份從1987年至2011年不等。我似乎無法獲得JSON加載,所以任何人都可以指向正確的方向?
var PhishVids = {
Models: {
Show: Backbone.Model.extend({
defaults: {
showid: 'show id',
year: 'year',
month: 'month',
day: 'day',
venue: 'venue'
}
})
},
Views: {
Show: Backbone.View.extend({
el: $('#content'),
initialize: function() {
this.model.fetch();
this.model.bind('change', this.render, this);
},
render: function(event) {
var compiled_template = _.template($("#shows-template").html());
this.el.html(compiled_template(this.model.toJSON()));
return this; //recommended as this enables calls to be chained.
},
events: {
"click .show": "showClick"
},
showClick: function(event) {
}
})
},
Collections: {
ShowList: Backbone.Collection.extend({
parse: function(response) {
return response.items;
}
})
}
};
顯示模板:
<script type="text/template" id="shows_template">
<a href='/<%= year %>/<%= month %>/<%= day %>' class='show <%= month %> <%= day %>' id='<%= showid %>'><%= month %>/<%= day %></a>
<div class='venue'><%= venue %></div>
</script>
在JS中你有'#show-template',模板中id是'show_template'。 – kubetz 2011-12-23 02:08:29
你是對的,謝謝。但是這並不能完全解決我的問題。 – switz 2011-12-23 02:12:19