我需要视图重新获取集合并每 30 秒重新渲染一次。问题是,一旦我更改页面(没有整页刷新),setInterval 就会保留在内存中并在后台继续重新获取。然而,这种观点早已被破坏。
代码:
define(
[
"underscore",
"collection/system/feed",
"view/list",
"text!template/index/system-feed.html"
],
function (_, Collection, ListView, Template) {
return ListView.extend({
el: '<div id="main-inner">',
collection: new Collection(),
loading: true,
client: false,
initialize: function (options) {
this.collection.fetch();
this.collection.on('reset', this.onFetchCollection, this);
var self = this;
setInterval(function() {
self.collection.fetch();
}, 30000);
},
/*collection is returned, so reset the loading symbol and set the posts to the render*/
onFetchCollection: function () {
this.list = this.collection.toJSON();
this.loading = false;
this.render();
},
render: function () {
var html = _.template(Template, {loading: this.loading, client: this.client, list: this.list});
this.$el.html(html);
return this;
}
});
}
);