Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow operation with dense array for Backbone collection #2802

Merged
merged 2 commits into from
Oct 16, 2013
Merged

allow operation with dense array for Backbone collection #2802

merged 2 commits into from
Oct 16, 2013

Conversation

jdkanani
Copy link
Contributor

It throws TypeError for collection operations with Backbone v1.1.0.

//  TypeError: Cannot read property 'id' of undefined
new Backbone.Collection(new Array(5)); 
col.add([undefined, {label: 'a'}]);
col.reset([{label: 'a'}, undefined, new Backbone.Model()]);

@akre54
Copy link
Collaborator

akre54 commented Oct 15, 2013

I'm not sure we should encourage passing undefined models to a collection since it will screw up most of the collection's methods. What's your use case?

@jdkanani
Copy link
Contributor Author

I agree. But, it was supported till Backbone v1.0.0. You can try that out same example with Backbone v1.0.0 and v1.1.0. And TypeError should not be thrown in any case.

@akre54
Copy link
Collaborator

akre54 commented Oct 15, 2013

In that case I'd consider it a fixed bug in Backbone 1.0.0. Absent a good use case, I think this should stay fixed.

@akre54 akre54 closed this Oct 15, 2013
@tgriesser
Copy link
Collaborator

This was introduced by the changes in #2755... Although this behavior wasn't documented, it might not be bad to either discard the empty values (as is done elsewhere, like events), or assume that an empty value is an empty object, as would be the case in this pull request.

@jashkenas jashkenas reopened this Oct 15, 2013
@tgriesser
Copy link
Collaborator

Since you can do new Model(null) and have it create an empty model, I think that just letting the falsy values pass along to _prepareModel makes sense as the behavior here, and the use case of new Collection(new Array(n)) is valid for a simple way of creating a collection with a particular length.

@jdkanani - can you add a few more tests to cover the cases you provided above?

@jashkenas
Copy link
Owner

We'll need a better implementation, though.

@tgriesser
Copy link
Collaborator

} else if (_.isObject(attrs)) { ?

@jashkenas
Copy link
Owner

No, something clearer and less incidental.

attrs = models[i] || {};

Should do it.

@tgriesser
Copy link
Collaborator

Oh right, of course :)

@jdkanani
Copy link
Contributor Author

@tgriesser @jashkenas Added one more commit for test cases and better fix.

jashkenas added a commit that referenced this pull request Oct 16, 2013
allow operation with dense array for Backbone collection
@jashkenas jashkenas merged commit 98e271b into jashkenas:master Oct 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants