骨干网/下划线链方法,其中方法 [英] Backbone / Underscore chain method with where method

查看:175
本文介绍了骨干网/下划线链方法,其中方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须有简单的东西,我在这里失踪。

http://jsfiddle.net/v9mdZ/

我刚学骨干,并强调/ loDash,我试图让熟悉

我有以下的code,符合市场预期其作品:

  VAR IDS = _.pluck(collection.where({'is_checked:真正}),ID);

我试图重构这个,使用像这样:

  VAR IDS = collection.chain(),其中({'is_checked:真正})摘去(ID)值()。

为什么不重构code的工作?我使用错了?

解决方案(详情如下)

不要使用,其中


解决方案

一些下划线的方法收集成有一点不完美。当你说 collection.some_mixed_in_underscore_method(),收集解开你的一些背后的东西骨干,使下划线方法应用于该系列的机型里面的属性;它排序是这样的:

  VAR元= _(this.models).MAP(功能(M){返回m.attributes});
返回_(进制).some_mixed_in_underscore_method();

collection.chain()不喜欢的工作,只是包装集合的的 模式 直接,所以如果你这样做:

 的console.log(collection.chain());

您将看到是给你一个包装的车型阵列的对象。您的车型将不会有一个 is_checked 属性(即不存在 model.is_checked ),他们将有 is_checked 虽然属性(即会有 model.get('is_checked') model.attributes .is_checked )。

现在我们能看到一切错误的:

  collection.chain(),其中({'is_checked:真正})。

该机型不具备 is_checked 属性。特别是,不会有其中 is_checked 是任何车型真正和一切后,其中,正在与空数组。

现在,我们知道的东西横着走,我们怎么解决这个问题?嗯,你可以使用 过滤器 代替的 ,其中 ,让您可以轻松解开型号:

  collection.chain()
          .filter(功能(M){返回m.get('is_checked')})
          .pluck(ID)
          。值();

不过,您的型号没有 ID 取值但是作为没有用 ID创建它们取值你还没有谈到一个服务器,以获得 ID 如此,你会得到一个数组未定义背部。如果你添加一些 ID 取值:

  VAR收集=新App.OptionCollection([
    {ID:1,'is_checked:真正},
    {ID:2,'is_checked:真正},
    {ID:3,is_checked:虚假}
]);

然后你会得到 [1,2] ,你要寻找的。

演示: http://jsfiddle.net/ambiguous/kRmaD/

There has to be something simple I am missing here.

http://jsfiddle.net/v9mdZ/

I am just learning Backbone and Underscore/loDash and am trying to get familiar with chain.

I have the following code, which works as expected:

var ids = _.pluck(collection.where({'is_checked':true}), 'id');

I attempted to refactor this, using chain like so:

var ids = collection.chain().where({'is_checked':true}).pluck('id').value();

Why doesn't the refactored code work? Am I using chain wrong?

Solution (details below)

Don't use where with chain.

解决方案

The merging of some Underscore methods into collections is a little imperfect. When you say collection.some_mixed_in_underscore_method(), the collection unwraps some of the Backbone stuff behind your back so that the Underscore method is applied to the attributes inside the collection's models; it sort of works like this:

var ary = _(this.models).map(function(m) { return m.attributes });
return _(ary).some_mixed_in_underscore_method();

But collection.chain() doesn't work like that, chain just wraps the collection's models directly so if you do this:

console.log(collection.chain());

you'll see that chain is giving you an object that wraps an array of models. Your models won't have an is_checked property (i.e. there is no model.is_checked), they will have is_checked attributes though (i.e. there will be model.get('is_checked') and model.attributes.is_checked).

Now we can see where everything goes wrong:

collection.chain().where({'is_checked':true})

The models don't have is_checked properties. In particular, there won't be any models where is_checked is true and everything after the where is working with an empty array.

Now that we know where things go sideways, how do we fix it? Well, you could use filter instead of where so that you can easily unpack the models:

collection.chain()
          .filter(function(m) { return m.get('is_checked') })
          .pluck('id')
          .value();

But, your models don't have ids yet as you didn't create them with ids and you haven't talked to a server to get ids so you're going to get an array of undefineds back. If you add some ids:

var collection = new App.OptionCollection([
    {id: 1, 'is_checked': true},
    {id: 2, 'is_checked': true},
    {id: 3, 'is_checked': false}
]);

then you'll get the [1,2] that you're looking for.

Demo: http://jsfiddle.net/ambiguous/kRmaD/

这篇关于骨干网/下划线链方法,其中方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆