_.pluck的骨干网集合不起作用 [英] _.pluck of Backbone collection not working

查看:47
本文介绍了_.pluck的骨干网集合不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个模特-

class pt.SearchResultModel extends Backbone.Model
  defaults:
    id:"",
    image:"",
    colour:""

我正在尝试-

_.pluck(resultsCollection,'id')

但是它总是返回未定义的-不确定发生了什么.

But it keeps returning undefined - not sure what's going on.

我犯了什么语法错误?

推荐答案

Underscore数组方法被嵌入到Backbone集合中(可以这么说).您可以直接在它们上调用它们:

The Underscore array methods are embedded (so to speak) into Backbone collections. You can call them directly on them:

resultsCollection.pluck 'id'

在大多数情况下,您还可以在集合的 models 属性(这是一个普通的数组)上使用Underscore方法,例如 _.pluck someCollection.models,'someAttr',但请注意 pluck 的情况很特殊,因为Backbone模型通常不会将其属性作为自己的属性(您必须调用非常简单不过:)

In most of the cases you could also use the Underscore methods over the collection's models attribute (which is a plain-old array), like _.pluck someCollection.models, 'someAttr', but notice that the case of pluck is special, as Backbone models will usually not have their attributes as own properties (you have to call get to access them). The implementation of Backbone's pluck is very straightforward nevertheless :)

这篇关于_.pluck的骨干网集合不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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