Backbone 多个属性 object inside objects
Backbone multiple atributtes with object inside objects
我正在尝试使用 findWhere 过滤我的 collection。过滤器 full_empty 和 idprovide 工作正常,但我不知道汽车类别的过滤器。
我需要过滤器
我的collection:
{
"full_empty": "0",
"idprovider": "AA",
"car": {
"category": "LL"
}
代码
model = coleccion.findWhere({
full_empty: 0,
idprovider: data_provider,
car: {category: data_category}
});
为什么不直接这样做:
var coleccion = new Backbone.Collection();
var data_provider = 'AA';
var data_category = 'LL';
coleccion.add({"full_empty": "0","idprovider": "AA","car": {"category": "LL"}});
coleccion.add({"full_empty": "0","idprovider": "AB","car": {"category": "LY"}});
coleccion.add({"full_empty": "0","idprovider": "AC","car": {"category": "LY"}});
console.log(coleccion.findWhere(function(_model){
var model = _model.attributes;
if (model.full_empty == 0 && model.idprovider == data_provider && model.car.category == data_category){
return _model;
}
}));
输出:
B…e.Model {cid: "c41", attributes: Object, collection: B…e.Collection, _changing: false, _previousAttributes: Object…}
注意 _model
与 model
变量。
PS。我使用 ==
而不是 ===
因为在你的例子中你正在比较 0
和 "0"
尝试使它尽可能统一因为 ==
有时会转身并咬你的屁股。 -- 你已被警告。
我正在尝试使用 findWhere 过滤我的 collection。过滤器 full_empty 和 idprovide 工作正常,但我不知道汽车类别的过滤器。 我需要过滤器
我的collection:
{
"full_empty": "0",
"idprovider": "AA",
"car": {
"category": "LL"
}
代码
model = coleccion.findWhere({
full_empty: 0,
idprovider: data_provider,
car: {category: data_category}
});
为什么不直接这样做:
var coleccion = new Backbone.Collection();
var data_provider = 'AA';
var data_category = 'LL';
coleccion.add({"full_empty": "0","idprovider": "AA","car": {"category": "LL"}});
coleccion.add({"full_empty": "0","idprovider": "AB","car": {"category": "LY"}});
coleccion.add({"full_empty": "0","idprovider": "AC","car": {"category": "LY"}});
console.log(coleccion.findWhere(function(_model){
var model = _model.attributes;
if (model.full_empty == 0 && model.idprovider == data_provider && model.car.category == data_category){
return _model;
}
}));
输出:
B…e.Model {cid: "c41", attributes: Object, collection: B…e.Collection, _changing: false, _previousAttributes: Object…}
注意 _model
与 model
变量。
PS。我使用 ==
而不是 ===
因为在你的例子中你正在比较 0
和 "0"
尝试使它尽可能统一因为 ==
有时会转身并咬你的屁股。 -- 你已被警告。