我在我的fetch
调用(在我的集合中)添加了对setInterval
的调用,并且它创建了一个意想不到的副作用。这里是正在调用集合的观点:Backbone.js和setInterval:如何检查DOM中是否有东西
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进行比较。这是最好的方法吗?如果是这样,我该怎么做?
谢谢彼得。我知道必须有办法做到这一点。 – sehummel 2013-03-12 21:59:25
现在我听到了,第二部分听起来有点粗略。我最喜欢你的第一种方法。再次感谢。 – sehummel 2013-03-12 22:05:25