显示骨干js过滤器
Backbone js filters display
我有一个带过滤器的项目集合。过滤器是基于几个复选框应用的。如果用户选择不同的月份(刷新项目),则项目集合会发生更改。
我的方法是在每个项目上循环,然后如果该项目是匹配的,则对该项目应用过滤器。显示项目或隐藏项目。这种方法在加载期间适用于当前月份,但当选择另一个月份时,将执行循环,如果条件评估为false,则仍执行该条件。我知道这个条件是false,因为当我在条件上放置断点时,我可以在控制台中看到该语句的求值结果为false。即使为false,下一步也会进入条件。
这是代码。
render: function() {
display = this.model.get('display');
filters = this.model.get('filters');
//Check each class for filter parameters
items.forEach(function (cla) {
//reset all items
$("#" + cla.id).parent().removeClass('matches');
$("#" + cla.id).parent().removeClass('noMatches');
//Get all filters
var hasCredit = "credits";
var hasLocation = "location";
var hasStarttime = "starttime";
var hasAreaOfStudy = "areaOfStudy";
var hasSpecialty = "specialty";
var hasEventCode = "eventcode";
var hasFormat = "format";
var itemFilters = [];
if (filters.inputs.length == 0) {
$("#" + cla.id).parent().addClass('matches');
} else {
//Loop over each filter
filters.inputs.forEach(function (el) {
//Check credit filter
if (el.name == 'credits') {
if (cla.credits.trim() == el.value) {
itemFilters.push(hasCredit);
}
} else if (el.name == 'specialty') {
if (jQuery.inArray(el.value.trim(), cla.specialty) !== -1) {
itemFilters.push(hasSpecialty);
}
} else if (el.name == 'areaOfStudy') {
if (jQuery.inArray(el.value, cla.areaOfStudy) !== -1) {
itemFilters.push(hasAreaOfStudy);
}
} else if (el.name == 'starttime') {
if (cla.starttime.trim() == el.value) {
itemFilters.push(hasStarttime);
}
} else if (el.name == 'location') {
if (cla.location.trim() == el.value) {
itemFilters.push(hasLocation);
}
} else if (el.name == 'eventcode') {
if (cla.id.trim() == el.value.trim()) {
itemFilters.push(hasEventCode);
}
} else if (el.name == 'format') {
if (cla.format == el.value) {
itemFilters.push(hasFormat);
}
}
});
var isInputFilterMatch = applyInputFilter(filters.inputs, itemFilters);
//All non matching filters get a noMatch css classed applied to be hidden
if (isInputFilterMatch) {
$("#" + cla.id).parent().addClass('matches');
} else {
$("#" + cla.id).parent().addClass('noMatches');
}
}
});
//Hide and show all matches and non matches
$('.noMatches').hide();
$('.matches').show();
$('#main_main_MainContent_CpeCalendar').parent('.hidden-results').show();
// shifted over to apply this after the items are set
jQuery_1_4_2(".modal[href], .modal a[href]").colorbox();
return this;
}
我认为有更好的方法可以实现这一点,但我将尝试回答您的问题。
首先,此代码似乎是View渲染方法的一部分。由于您还没有从视图、集合或模型中提供任何其他代码,这让我不禁要问,您是否正确地将视图重新绑定到集合中的更新数据?如果没有,那么基于您提供的这段代码,我认为您可能需要重新呈现视图,以便在收集数据逐月变化时正确地加载所有数据/事件绑定,因为它可能会丢失:
$('#main_main_MainContent_CpeCalendar').parent('.hidden-results').show();
// shifted over to apply this after the items are set
jQuery_1_4_2(".modal[href], .modal a[href]").colorbox();
return this;
与其把所有这些都放在View的render()
方法中,为什么不把这些代码分解成更有逻辑意义的代码,并使用Backbone给你的功能?下面是一个使用集合本身过滤Backbone集合的示例(这是一种更合适、更可维护的方法)。
处理对主干网的筛选。从主干网中筛选集合的项目。集合本身,并使用其与数据库的方便连接(充当视图和构成集合的模型之间的同步),如果用户通过UI启动状态更改以确保数据完整性,而不是您在这里所做的,这将非常方便。
如果您能够提供集合、模型和视图中的全部代码,我可能能够为您提供这个问题的更好答案。根据我在这里看到的情况,有太多的变量我不知道要解决。
- 如何在angular.js中动态应用自定义过滤器
- DC.js带过滤器的订购
- Angular js过滤器在某些移动设备上不起作用
- Vue.js如何在定义API变量之前实现自定义过滤器
- (角度.js)如何通过过滤器计算数组中有多少项目
- 在Angular.JS-ng隐藏vs过滤器中将对象分隔为两组
- 如何在Angular js中返回html代码作为过滤器的输出
- 显示骨干js过滤器
- 在kibana3/elastic.js中合并两个过滤器
- Angular js自定义过滤器未定义
- 自定义Angular.js过滤器在IE8中不返回任何内容,在Chrome/FF中运行良好
- 如何将Underscore.js过滤器与对象一起使用
- Vue js 在输入字段中对 v-model 应用过滤器
- 如何使用fabric.js添加图像过滤器
- 如何使用angular js过滤器格式化数组中的值
- 为织物形状的图案图像添加过滤器.js
- 过滤器.js在 ReactJS 中
- Gettin 交叉过滤器.js错误“太多递归”
- 如何简化用下划线编写的过滤器.js
- 简单条形图按字符串名称分组,带有 DC.js D3.js 和交叉过滤器.js