Skip to content

Add checks for null collections #202

Closed
michaelbrooks opened this Issue Sep 30, 2012 · 6 comments

5 participants

@michaelbrooks

Apparently, underscore.js no longer checks if collections are null. The topic is discussed in this issue: jashkenas/underscore#744

In my application, this is causing an error on line 924 where _.any is used to find an entity in this.keyContents, but this.keyContents is undefined.

Specifically, once _.any is called, the check if obj.some === nativeSome fails because obj is undefined.

The fix for this could be something like:

// Check if this new model was specified in 'this.keyContents'
var item = _.any(this.keyContents, function(item) {

to

// Check if this new model was specified in 'this.keyContents'
var item = _.any(this.keyContents || [], function(item) {

I'm guessing there are other places in backbone-relational that need to be updated as well, though. Someone else should probably take a deeper look at this than I can right now.

@philfreo
Collaborator

yep... backbone-relational is broken on underscore 1.4.0 :(

also _.each( models , function( related ) { in getReverseRelations throws an error now when models is null

@philfreo
Collaborator

though for what it's worth, I think those 2 changes are the only places that needed to be changed (at least with obvious errors for me)

@wprl
wprl commented Oct 1, 2012

This thread was really helpful!

@wprl
wprl commented Oct 1, 2012

Added pull request that adds the || []s

@jdalton
jdalton commented Oct 4, 2012

Just a heads up, Lo-Dash v0.8.1 won't error when falsey values are passed to collection methods. So you can use it w/o having to modify your project's code.

@PaulUithol
Owner

Has been fixed, thanks all!

@PaulUithol PaulUithol closed this Dec 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.