Underscore.js "some" not return boolean

I am using underscore.js to check if an item is true in a list. Here is the coffeescript code for it:

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

      

printing 'uploading' instead of returning true or false gives:

uploading
  y
   _chain: true
   _wrapped: false
   __proto__: Object

      

Here is the definition of "some" for underscore: http://underscorejs.org/#some .

Also, getViews is a function from the base layout, defined here: https://github.com/tbranyen/backbone.layoutmanager/wiki/Nested-views

Here is the result of other variables that can make debugging easier:

_(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)}

      

+3


source to share


2 answers


If you look getViews

, you can see what's going on:

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

      

If you look at all the possible return values, you can see that they are the result of _.chain

calls without a _.value

call to unwrap the chain. This means it getViews

returns a chained Underscore wrapper, not the simple array you would expect.

You shouldn't _(@getViews '...')

, since the return value is getViews

already wrapped in Underscore. You should do something like this:



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

      


As an aside, I am a little wary of your test v.uploading is true

; explicit checks are against true

and false

may cause odd behavior (especially in CoffeeScript, where is

indeed ===

). I would use a (v) -> v.uploading

function instead . Of course, this is personal preference.

+2


source


getViews

seems to return a pre-wrapped and chained underline object for your consumption. Calling _

on this again does nothing in this circumstance. You can put uploading.value()

to get the desired result.



+2


source







All Articles