Backbone.js 主干集合-构造函数
我正在将数组传递给新集合。是否可以通过传递第二个参数来过滤数组,并检查数组中对象的attr,并且仅当它通过过滤测试时才创建集合Backbone.js 主干集合-构造函数,backbone.js,Backbone.js,我正在将数组传递给新集合。是否可以通过传递第二个参数来过滤数组,并检查数组中对象的attr,并且仅当它通过过滤测试时才创建集合 collection = new Backbone.Collection([{name:'xy',age:24},{name:'y',age:35}]) 我只能为30年以下的对象创建集合。在进入集合之前,您可以在阵列上使用 collection = new Backbone.Collection( _.filter([{name:'xy',age:24},{
collection = new Backbone.Collection([{name:'xy',age:24},{name:'y',age:35}])
我只能为30年以下的对象创建集合。在进入集合之前,您可以在阵列上使用
collection = new Backbone.Collection(
_.filter([{name:'xy',age:24},{name:'y',age:35}],
function(item) { return item.age < 30; }));
演示
您可以通过在options参数中提供过滤器谓词来扩展此功能
var FilteredCollection = Backbone.Collection.extend({
initialize: function(models, options) {
for(var i = models.length - 1; i > 0; i-- ){
if (options.filter && options.filter(models[i])){
models.splice(i,1);
}
}
}
});
和它一起使用
var collection = new FilteredCollection(
[{name:'xy',age:24}, {name:'y',age:35}],
{
filter: function(item) {
return item.age > 30;
}
}
);
console.log(collection.models.length); // will be 1
更新
您对创建集合之前的验证感兴趣,就像前面的答案一样-我们将创建一个函数,允许您重用该策略,根据您喜欢的条件验证任何集合
//A collection that validates the correctness of the given elements
// based on a predicate (condition). If the items do not _all_ pass
// the condition - the collection will not be created and an error will
// be thrown.
function validatingCollection(predicate){
return function(arr){
//first we assert an array is passed if it's possible to assert
if(Array.isArray && !Array.isArray(arr)){
throw new Error("Can only pass array to filteredCollection");
}
//for older browsers - use _.every
// now we validate they all pass the condition
if(!arr.every(predicate){
throw new Error("Validation error, not all elements pass");
}
//they all pass, create the collection
return new Backbone.Collection(arr);
};
};
现在,我们可以创建这样一个集合,验证每个元素的年龄至少为30岁:
var ageValidated = validatedCollection(function(obj){ return obj.age < 30; });
//create a new age validatedcollection
try{
var collection = new ageValidated([{name:'xy',age:24},{name:'y',age:35}]);
}catch(e){
// the above throws an error because of the object with age 24
}
//however - this works, and creates a new collection
var collection = new ageValidated([{name:'xy',age:32},{name:'y',age:35}]);
这让我们可以做到:
var ageFilter = filteredCollection(function(obj){ return obj.age < 30; });
//create a new age collection
var collection = new ageFilter([{name:'xy',age:24},{name:'y',age:35}]);
var ageFilter=filteredCollection(函数(obj){return obj.age<30;});
//创建新时代收藏
var collection=newagefilter([{name:'xy',age:24},{name:'y',age:35}]);
另一个答案建议使用
\uu.filter
而不是本机Array.filter,如果您必须支持旧浏览器,这可能会很有用。您可以添加一些代码吗?根据您的描述,很难理解您想要实现什么…我将从任何位置传递数组..我希望集合能够处理它..在创建collection@cool您可以将此自定义筛选添加到集合初始化方法中。我更新了我的答案。我想我可以在初始化中使用splice来删除不必要的模型。我可以使用过滤函数,但与收集/初始化验证相比,这将是一个难以代码维护的问题。。但是仍然有一个问题,我们可以在创建/构造库的过程中在collection/model中添加验证吗..我的问题是,在创建集合之前,集合构造中是否可以进行验证…@coool我更新了我的解决方案,重点是代码重用。此外,您可能需要考虑将对象本身放在<代码>主干中。模型并将验证放在模型上(可能有<代码>人>代码>模型,然后是<代码> OLDENMENT/<代码>模型,并在那里处理验证,或者根据您想要完成的不同而不使用主干。
collection = new Backbone.Collection([{name:'xy',age:24},
{name:'y',age:35}].filter(function(obj){
return obj.age < 30;
});
function filteredCollection(predicate){
return function(arr){
//first we assert an array is passed if it's possible to assert
if(Array.isArray && !Array.isArray(arr)){
throw new Error("Can only pass array to filteredCollection");
}
return new Backbone.Collection(arr.filter(predicate));
};
};
var ageFilter = filteredCollection(function(obj){ return obj.age < 30; });
//create a new age collection
var collection = new ageFilter([{name:'xy',age:24},{name:'y',age:35}]);