在集合中引用变量时出现问题.方法在渲染函数中
Trouble referencing variable in Collections.where method within render function
我在主干代码中遇到了一些麻烦。下面的代码涉及到一个呈现函数。我可以找回所有的模型。当我尝试使用"集合"时,问题就出现了。方法位于编号为#1的行。正如您所看到的,我已经将一个对象文字传递给了渲染函数,但是由于某种原因,我无法在客户中引用它。方法在第1行。当我给这个方法一个字面数字,比如45,它能工作。有没有什么方法可以让我把变量引用传递进去?
谢谢
render: function(options) {
var that = this;
if (options.id) {
var customers = new Customers();
customers.fetch({
success: function (customers) {
/* #1 --> */ var musketeers = customers.where({musketeerId: options.id});
console.log(musketeers.length) //doesn't work as options.id is failing on last line
var template = _.template($('#customer-list-template').html(), {
customers: customers.models
});
that.$el.html(template);
console.log(customers.models);
}
});
} else {
var template = _.template($('#customer-list-template').html(), {});
that.$el.html(template);
}
}
虽然没有明确记录,但Collection#where
在搜索时使用严格相等(===
)。来自精美的源代码:
where: function(attrs, first) {
if (_.isEmpty(attrs)) return first ? void 0 : [];
return this[first ? 'find' : 'filter'](function(model) {
for (var key in attrs) {
if (attrs[key] !== model.get(key)) return false;
}
return true;
});
},
注意回调函数中的attrs[key] !== model.get(key)
,它不会考虑10
(可能的id
值)和'10'
(从<input>
中提取的可能的搜索值)是匹配的。这意味着:
customers.where({musketeerId: 10});
可能会找到一些东西,而
customers.where({musketeerId: '10'});
不会。
你可以用parseInt
:
// Way off where you extract values from the `<input>`...
options.id = parseInt($input.val(), 10);
相关文章:
- 你能把一个匿名方法(函数)动态地变成一个命名方法吗
- 显示警报后调用方法/函数
- 使用来自 json 对象的参数动态调用方法/函数
- 创建具有动态参数排列的方法/函数
- 如何从其他 jsp 调用方法/函数以包含在另一个 jsp 的 jstree 中
- Objective-C 方法/函数调用.(来自JavaScript示例)
- 如何在android phongap应用程序中从javascript调用本机android方法(函数),而无需使用web
- 如何测试函数是否调用了特定的方法/函数
- 处理setTimeout(javascript)中的对象方法函数调用
- 简单级别,在DOM中选择元素,优化,创建方法函数
- 在不使用eval的情况下在方法函数中传递固定参数
- jQuery.html()方法函数文字参数
- animate方法函数执行意外延迟
- 对象方法javascript中的方法/函数
- Javascript私有方法:函数表达式与函数声明
- Jquery的oop重写方法函数
- 如何为数组/对象创建方法/函数
- 是否有可能发送我的对象与方法(函数)从服务器到客户端
- Javascript可以调用Django方法/函数吗
- 访问Backbone.js中的Ratchet 2.0方法/函数