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

Collection.get returns Object.prototype methods #2973

Closed
jthoms1 opened this issue Jan 28, 2014 · 5 comments
Closed

Collection.get returns Object.prototype methods #2973

jthoms1 opened this issue Jan 28, 2014 · 5 comments

Comments

@jthoms1
Copy link

jthoms1 commented Jan 28, 2014

Currently Backbone.Collection is using an object to keep track of models that exist within that collection. It should probably be using 'hasOwnProperty' instead to avoid returning methods from Object.prototype.

The issue originally occurred for me in Firefox when I had a model id of 'watch' and backbone refused to add it to a new collection. This was using 0.9.9 but it appears the issue exists within the latest version as well.

I have a jsbin to demonstrate the issue with 'valueOf'.

http://jsbin.com/OmeBAW/3/edit

@akre54
Copy link
Collaborator

akre54 commented Jan 29, 2014

Hey @jthoms1, this has been brought up a few times before (do a search through past tickets). Unfortunately thats the tradeoff when using an object to store these keys. Dont allow arbitrary values for your ids (and stick with numeric, unique, and sequential ids) and you should be fine.

@akre54 akre54 closed this as completed Jan 29, 2014
@ghost
Copy link

ghost commented Jan 29, 2014

Since there is already get and set api access points to the data can't internally all properties simply be prefixed? Like:

 get: function(attr) {
   return this.attributes['somePrefix' + attr];
 }

or better yet as @jthoms1 suggested using _.has:

 get: function(attr) {
   return _.has(this.attributes, attr) ? this.attributes[attr] : void 0
 }

with the same thing done in the set?

@akre54
Copy link
Collaborator

akre54 commented Jan 29, 2014

@Dw40 that doesnt seem like a good solution for either Model or Collection to me (too much overhead -- cognitive and otherwise). See other threads for more discussion and some suggestions.

@jthoms1
Copy link
Author

jthoms1 commented Jan 29, 2014

Apologizes, I did not see previous tickets. Issue #2719 covers my exact scenario.

I was going to ask for a documentation update but alas current documentation references "(integer id or UUID)" to be used as model IDs. Thanks

@akre54
Copy link
Collaborator

akre54 commented Jan 29, 2014

No problem. Thats what its there for :)
On Jan 29, 2014 5:33 PM, "Josh Thomas" [email protected] wrote:

Apologizes, I did not see previous tickets. Issue #2719https://github.com/jashkenas/backbone/issues/2719covers my exact scenario.

I was going to ask for a documentation update but alas current
documentation references "(integer id or UUID)" to be used as model IDs.
Thanks

Reply to this email directly or view it on GitHubhttps://github.com//issues/2973#issuecomment-33594789
.

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

No branches or pull requests

2 participants