Backbone 新手,请承受我不太漂亮的主干 javascript 代码。
这是我的代码
var Schedule = Backbone.Model.extend({
initialize: function () {
console.log("initializing model");
}
});
var ScheduleCollection = Backbone.Collection.extend({
model: Schedule,
url: "<%=students_swimming_classschedules_path%>",
parse: function (resp) {
return resp;
},
});
var Schedules = Backbone.View.extend({
initialize: function () {
console.log("initializing view");
collection.on('add', this.render, this);
this.render();
},
render: function () {
for (var i = 0; i < collection.length; i++) {
s += "<tr><td>" + collection.models[i].get('account') + "</td><td>" + collection.models[i].get('status') + "</td></tr>";
}
this.$el.html(s);
},
})
var schedules = new Schedules({
el: $("#students")
});
window.setInterval(function () {
collection.fetch();
}, 2000);
此代码有效。我可以将所有学生加载到 $('#students') 容器中。
但我想每隔几秒钟从服务器和视图中自动重新加载集合。任何帮助,将不胜感激。