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

Added support for number type attributes #68

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions ampersand-select-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,23 +351,33 @@ module.exports = View.extend({
* @return {*} string, array, state, or model
*/
getOptionByValue: function(value) {
var model;
var model, i;
if (this.options.isCollection) {
// find value in collection, error if no model found
if (this.options.indexOf(value) === -1) model = this.getModelForId(value);
else model = value;
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];
if (this.options.length > 0) {
// find value in collection as [['val','text'], ...]
if (Array.isArray(this.options[0])) {
for (i = this.options.length - 1; i >= 0; i--) {
if (this.options[i][0] == value) {
return this.options[i][0];
}
}
// find value in collection as ['val', ...]
} else {
for (i = this.options.length - 1; i >= 0; i--) {
if (this.options[i] == value) {
return this.options[i];
}
}
}
} else {
throw new Error('no options provided');
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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');
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,20 @@ suite('Options array with array items', function (s) {
}
}));

s.test('selects the right number item (options: [[number, \'text\']])', sync(function (t) {
view = new SelectView({
autoRender: true,
name: 'number',
options: arrNum,
value: 0
});

var select = view.el.querySelector('select');

view.setValue(1.5);
t.equal(select.options[select.selectedIndex].value, '1.5');
}));

s.test('renders a disabled item if a third value is passed which is truthy', sync(function (t) {
view = new SelectView({
autoRender: true,
Expand Down