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 3 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
25 changes: 18 additions & 7 deletions ampersand-select-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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--) {
Copy link
Contributor

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 statement i >= 0 can simply be this.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.

Copy link
Author

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:

    getOptionByValue: function(value) {
        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)) {
            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');
            }
            throw new Error('value not in set of provided options');
        }
        throw new Error('select option set invalid');
    },

Copy link
Author

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.

Copy link
Contributor

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.

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');
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