3

我正在使用 underscore.js 来检查列表中的元素是否为真。这是它的咖啡脚本代码:

uploading  = _(@getViews ".file-forms").some (view) ->
    view.uploading is true

打印“上传”,而不是返回真或假,给出:

uploading
  y
   _chain: true
   _wrapped: false
   __proto__: Object

以下是下划线“some”的定义:http: //underscorejs.org/#some

此外,getViews 是此处定义的主干布局管理器的功能:https ://github.com/tbranyen/backbone.layoutmanager/wiki/Nested-views

以下是其他变量的输出,可能会使调试更容易:

_(this.getViews(".file-forms"))
 y
  _chain: true
  _wrapped: Array[1]
  0: d
  length: 1
  __proto__: Array[0]
  __proto__: Object

_
 function (a){if(a instanceof y)return a;if(this instanceof y)this._wrapped=a;else return new y(a)}
4

2 回答 2

2

getViews似乎正在返回一个预先包装和链接的下划线对象供您使用。在这种情况下,再次调用_它没有任何作用。你可以把uploading.value()得到你想要的结果。

于 2013-01-31T00:02:02.460 回答
2

如果你看一下getViews,你会看到发生了什么:

getViews: function(fn) {
  //...
  if (typeof fn === "string") {
    return _.chain([this.views[fn]]).flatten();
  }
  //...
}

如果您查看所有可能的返回值,您会发现它们都是_.chain调用的结果,而没有_.value调用剥离链接包装器。这意味着这getViews将返回一个可链接的 Underscore 包装器,而不是您期望的简单数组。

您不应该这样做_(@getViews '...'),因为getViews返回值已经包含在下划线中。您应该可以这样做:

uploading = @getViews(".file-forms").some((view) -> view.uploading is true).value()

顺便说一句,我对你的v.uploading is true测试有点怀疑;显式检查truefalse可能导致奇怪的行为(尤其是在 CoffeeScript where isis really中===)。我可能会改用(v) -> v.uploadingas 函数。当然,这是个人喜好。

于 2013-01-31T00:01:36.637 回答