English 中文(简体)
后骨过滤过滤
原标题:Backbone filtering

如果我有一个 Backbone 收藏, 并且想要创建该收藏的复制件, 并过滤了某些条目, 我怎么能做到这一点, 同时把复制的例子保留为 Backbone 。 Collection?

示例:

var Module = Backbone.Model.extend();

var ModuleCollection = Backbone.Collection.调extend({
    model: Module
});

调var modules = new ModuleCollection;

调modules.add({foo:  foo 调调调调调调},{foo:  bar });调调调调调

console.log(modules instanceof Backbone.Collection); // true

var filtered = modules.filter(function(module) {
    return module.get( foo ) ==  bar ;
});

console.log(filtered instanceof Backbone.Collection); // false

http://jsfiddle.net/m9ety/" rel="nreferrer" >http://jsfiddle.net/m9ety/

在上述例子中,我希望 filtered 成为模块的过滤版本,而不仅仅是一系列模型。

基本上,我想在收集实例中创建一种方法,可以过滤某些模型并返回 Backbone.collection 实例,但只要我开始过滤迭代方法,就会返回数组。

最佳回答

您可以在临时模块集合中包装过滤的阵列, 如果您想要的话, 过滤的模型与原始模块集合中的模型相同, 所以如果模块的属性发生变化, 它仍然被两个收藏所引用 。

所以我建议你做的是:

var filtered = new ModuleCollection(modules.filter(function (module) {
    return module.get( foo ) ==  bar ;
}));

由于后骨0.9.2,还有一种叫的又一种方法,即 ,其中的 也这样做:

var filtered = modules.where({foo:  bar });

,仍然返回一个矩阵,所以你仍然需要将它包装成这样:

var filtered = new ModuleCollection(modules.where({foo:  bar }));
问题回答

用于使用主干进行过滤收集

要建立过滤器, 您的收藏中应该有一个过滤功能

var MyCollection = Backbone.Collection.extend ({
  filtered : function () { 

我建议使用 underScore 过滤器, 该过滤器将返回正确和错误的无效, 无效和无效, 如果您正在寻找的是真实的。 使用此选项。 模型可以让当前收藏模型使用模型. get () 来获取您要检查的元素 。

var results = _.filter( this.models, function ( model ) {           
    if ( model.get( foo ) ==  bar  ) 
    return true ; 
    return false ;
});

然后用突出显示显示您的结果, 并将其转换为 JSON, 就像这是你错的地方 。

results = _.map( results, function( model ) { return model.toJSON()  } );

最后还返回一个新的主干文架收藏, 仅有结果, 即如何复制收藏 。

return new Backbone.Collection( results ) ;




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签