我在一个应用程序中使用Backbone.js。我将段与url相关联。因此,一个段可以包含许多url,一个给定的url可以在任何段中。有一个url窗格和一个段窗格。问题是突出显示部分。因此,当我单击一个段时,我想突出显示它的url。我已将一个页面上显示的url数量限制为200。如果超过200个url'我们只向用户显示前200个网址,其余的网址,用户只使用实时搜索来找到他要找的网址。问题是,当网址少于200个时,当我点击一个分段时,高亮显示就起作用了。当网址超过200个时,当用户点击一个分段时,突出显示不起作用。我在集合上使用切片,如果有超过200个url,只突出显示前200个,但这不起作用。以下是代码片段。有人对如何解决这个问题有什么好的建议吗?
在toggleSelection
函数的SegmentView.js中:
toggleSelection: function() {
var max = 200;
//get the urls
var urls = this.App.segmentUrlCollection.urlsForSegment(this.model);
var pixels = this.App.segmentPixelCollection.pixelsForSegment(this.model);
if (this.selected) {
this.deselect();
this.selected = false;
this.$('.summary').removeClass('selected');
this.App.segmentCollection.each(function(segment){
if (segment.get('name') == "Unmapped"){
segment.view.$('.summary').addClass('unmapped');
}
});
//If there are more than 200 urls in url Collection just highlight the first 200.
if (this.App.urlCollection.size > 200) {
//problem?
this.App.urlCollection.slice(0,199).each(function(url) {
if (url.view.App.isUrlUnmapped(url)) {
url.view.$('.summary').addClass('unmapped');
}
});
}
else {
this.App.urlCollection.each(function(url) {
if (url.view.App.isUrlUnmapped(url)) {
url.view.$('.summary').addClass('unmapped');
}
});
}
//deselect the urls
_(urls).each(function(url) {
url.view.deselect();
});
_(pixels).each(function(pixel) {
pixel.view.deselect();
});
} else {
this.App.segmentCollection.each(function(segment) {
segment.view.selected = false;
segment.view.deselect();
});
this.App.segmentCollection.each(function(segment){
if (segment.view.$('.summary').hasClass('unmapped')){
segment.view.$('.summary').removeClass('unmapped');
}
});
//If there are more than 200 urls in url Collection just highlight the first 200.
if (this.App.urlCollection.size > 200) {
//problem?
this.App.urlCollection.slice(0,199).each(function(url) {
if (url.view.$('.summary').hasClass('unmapped')) {
url.view.$('.summary').removeClass('unmapped');
}
// url.view.deselect();
});
}
else {
this.App.urlCollection.each(function(url) {
if (url.view.$('.summary').hasClass('unmapped')) {
url.view.$('.summary').removeClass('unmapped');
}
// url.view.deselect();
});
}
//If there are more than 200 urls in url Collection just highlight the first 200.
if (this.App.urlCollection.size > 200) {
//problem?
this.App.urlCollection.slice(0,199).each(function(url) {
url.view.deselect();
});
}
else {
this.App.urlCollection.each(function(url) {
url.view.deselect();
});
}
this.App.pixelCollection.each(function(pixel) {
pixel.view.deselect();
});
this.select();
this.selected = true;
this.$('.summary').addClass('selected');
//select the urls
_(urls).each(function(url) {
url.view.select();
});
_(pixels).each(function(pixel) {
pixel.view.select();
});
}
return false;
}
1条答案
按热度按时间cwxwcias1#
您可能会遇到一个JavaScript错误,阻止您的代码运行,因为
slice
是数组的JavaScript方法。 Backbone.js 集合不是数组,因此该方法不存在。您可以通过调用
urlCollection.toArray()
从集合中获取模型数组的副本