javascript – 无法在render函数中引用Collections.where方法中的变量
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 无法在render函数中引用Collections.where方法中的变量,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1581字,纯文字阅读大概需要3分钟。
内容图文
![javascript – 无法在render函数中引用Collections.where方法中的变量](/upload/InfoBanner/zyjiaocheng/708/5e0df783f8f34b62bc2b02d09959f8cc.jpg)
我用一段骨干代码遇到了一些麻烦.下面的代码涉及渲染功能.我可以检索所有模型.当我尝试在标记为#1的行上使用“Collections.where”方法时,我的麻烦就出现了.如您所见,我已将对象文字传递给渲染函数,但由于某种原因,我无法在第1行的customers.where方法中引用它.当我给这个方法一个像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在搜索时使用strict equality (===).从fine source code:
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);
内容总结
以上是互联网集市为您收集整理的javascript – 无法在render函数中引用Collections.where方法中的变量全部内容,希望文章能够帮你解决javascript – 无法在render函数中引用Collections.where方法中的变量所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。