-
Notifications
You must be signed in to change notification settings - Fork 26
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
Added support for number type attributes #68
base: master
Are you sure you want to change the base?
Changes from 3 commits
3e07fa0
327ba1d
82ad163
7270f3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -359,15 +359,26 @@ module.exports = View.extend({ | |
if (!model) throw new Error('model or model idAttribute not found in options collection'); | ||
return this.yieldModel ? model : model[this.idAttribute]; | ||
} else if (Array.isArray(this.options)) { | ||
// find value value in options array | ||
// find option, formatted [['val', 'text'], ...] | ||
if (this.options.length && Array.isArray(this.options[0])) { | ||
for (var i = this.options.length - 1; i >= 0; i--) { | ||
if (this.options[i][0] == value) return this.options[i]; | ||
var i = this.options.length - 1; | ||
if (i >= 0) { | ||
// find value in collection as [['val','text'], ...] | ||
if (Array.isArray(this.options[0])) { | ||
for (i; i >= 0; i--) { | ||
if (this.options[i][0] == value) { | ||
return this.options[i][0]; | ||
} | ||
} | ||
// find value in collection as ['val', ...] | ||
} else { | ||
for (i; i >= 0; i--) { | ||
if (this.options[i] == value) { | ||
return this.options[i]; | ||
} | ||
} | ||
} | ||
} else { | ||
throw new Error('no options provided'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like and support this, we tend to want to throw exceptions when devs get themselves into places that would give unexpected results. This will mean a major release though because it would not have thrown in this case before. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think I should remove this for now and do a separate PR for it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope! Leave it. Versions are cheap I'm just making sure we see that so we don't miss it. |
||
} | ||
// find option, formatted ['valAndText', ...] format | ||
if (this.options.length && this.options.indexOf(value) !== -1) return value; | ||
throw new Error('value not in set of provided options'); | ||
} | ||
throw new Error('select option set invalid'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reusing the variable
i
here seems like the wrong move, it confuses its purpose imo.Honestly we could simplify it by scoping it (
var i;
) and then the if statementi >= 0
can simply bethis.options.length >= 1
Typically the
length
attribute is cast into its own var as an optimization for loops. It's only being referenced once during the if statement so it's really not necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this to avoid the
'i' is already defined.
error that the pre-commit hook throws. What do you think about this as a solution? Declaring i at the top of the function, and then setting it for each loop:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option would be to use a different var name for each loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your code example is exactly what I was trying to suggest, that will work, or using two variable names will work, not picky either way honestly.