我在我的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