麻烦引用变量Collections.where方法中渲染功能 [英] Trouble referencing variable in Collections.where method within render function
问题描述
我遇到了一些麻烦一块骨干code的。下面的code涉及渲染功能。我可以检索所有车型。我的麻烦出现,当我尝试使用Collections.where的方法以线条为标志号#1。正如你所看到的,我已经通过了一个对象类型,渲染功能,但由于某种原因,我无法就行#1 customers.where方法中引用它。当我把这个方法的字面数字,如45它的工作原理。有没有解决这个某种方式让我可以通过该变量引用?
很多
感谢 渲染:功能(选件){ VAR认为这=;
如果(options.id){ VAR的客户=新客户();
customers.fetch({
成功:函数(客户){
/ *#1 - > * / VAR火枪手= customers.where({musketeerId:options.id});
的console.log(musketeers.length)//不为options.id工作是失败的最后一行
VAR模板= _.template($('#客户列表的模板)。HTML(){
客户:customers.models
});
。该$ el.html(模板);
的console.log(customers.models);
}
}); }其他{
VAR模板= _.template($('#客户列表的模板)HTML(){});
。该$ el.html(模板);
}
}
尽管没有明确记载, 收藏#其中
用途的全等( ===
)搜索时。从href=\"http://backbonejs.org/docs/backbone.html#section-101\" rel=\"nofollow\">精源$ C $ C 的
其中:函数(ATTRS,第一){
如果(_.isEmpty(ATTRS))先返回?无效0:[];
返回此[第一? 发现':'过滤器'](函数(模型){
对(在ATTRS VAR键){
如果(!ATTRS [关键] == model.get(键))返回false;
}
返回true;
});
},
注意 ATTRS [关键] == model.get(键)
的回调函数内,不会考虑 10
(可能的 ID
值)和 '10'
(可能的搜索值从提取<输入>
)是一个匹配。这意味着,
customers.where({musketeerId:10});
可能会找到的东西,而
customers.where({musketeerId:'10'});
不会的。
您可以避开这样的事情与 parseInt函数
:
//的路要走,你从中提取`&LT值;输入>`...
options.id = parseInt函数($ input.val(),10);
I have run into some trouble with a piece of backbone code. The code below relates to a render function. I can retrieve all the models. My trouble arises when I try to use the "Collections.where" method at line marked number #1. As you can see, I have passed an object literal into the render function but for some reason I am unable to reference it within the customers.where method on line #1. When I give this method a literal number like 45 it works. Is there some way around this so I can pass the variable reference in?
Thanks alot
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);
}
}
Although it isn't explicitly documented, Collection#where
uses strict equality (===
) when searching. From the 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;
});
},
note the attrs[key] !== model.get(key)
inside the callback function, that won't consider 10
(a probable id
value) and '10'
(a probable search value extracted from an <input>
) to be a match. That means that:
customers.where({musketeerId: 10});
might find something whereas:
customers.where({musketeerId: '10'});
won't.
You can get around this sort of thing with parseInt
:
// Way off where you extract values from the `<input>`...
options.id = parseInt($input.val(), 10);
这篇关于麻烦引用变量Collections.where方法中渲染功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!