【发布时间】:2013-03-12 20:35:53
【问题描述】:
我在我的fetch 调用(在我的收藏中)中添加了对setInterval 的调用,它产生了意想不到的副作用。这是调用集合的视图:
define([
'jquery',
'underscore',
'backbone',
'models/tableModel',
'collections/tablesCollection',
'views/tableView'
],
function($, _, Backbone, tableModel, tablesCollection, tableView) {
var tablesView = Backbone.View.extend({
tagName: 'div',
initialize: function(options) {
this.collection.on('reset', this.render, this);
this.template = this.options.template;
this.url = this.options.url;
},
render: function() {
this.collection.each(this.addOne, this);
return this;
},
addOne: function(model) {
var TableView = new tableView({ model: model, template: this.template, url: this.url });
$(this.$el).append(TableView.render().el);
return this;
}
});
return tablesView;
});
如您所见,此视图遍历集合,将每个线索附加到 DOM,这正是我想要的。但是通过调用setInterval,每次获取集合时都会执行此操作。这就是我想要的,但我想在 append 之前添加一行以检查该项目是否已经在 DOM 中。我想我可以使用model.get('id') 并将其与 DOM 进行比较。这是最好的方法吗?如果是这样,我该怎么做?
【问题讨论】:
标签: jquery dom backbone.js