From 3e07fa0b4fb928ad65e4420aa320aed2adf68c09 Mon Sep 17 00:00:00 2001 From: Douglas Waltman II Date: Mon, 8 Feb 2016 17:38:18 -0800 Subject: [PATCH 1/4] Added support for number type attributes --- ampersand-select-view.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/ampersand-select-view.js b/ampersand-select-view.js index b5be384..91a294a 100644 --- a/ampersand-select-view.js +++ b/ampersand-select-view.js @@ -359,15 +359,20 @@ 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])) { + if (this.options.length) { for (var i = this.options.length - 1; i >= 0; i--) { - if (this.options[i][0] == value) return this.options[i]; + var option = this.options[i]; + // find value in collection as [['val','text'], ...] + if (Array.isArray(option) && option[0] == value) { + return option[0]; + // find value in collection as ['val', ...] + } else if (option == value) { + return option; + } } + } else { + throw new Error('no options provided'); } - // 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'); From 327ba1d121c6eafb08dda446a511c0c5c9a286f9 Mon Sep 17 00:00:00 2001 From: Douglas Waltman II Date: Tue, 9 Feb 2016 10:07:46 -0800 Subject: [PATCH 2/4] Removed support for mixed option types --- ampersand-select-view.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ampersand-select-view.js b/ampersand-select-view.js index 91a294a..b0b2f69 100644 --- a/ampersand-select-view.js +++ b/ampersand-select-view.js @@ -359,15 +359,21 @@ 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)) { - if (this.options.length) { - for (var i = this.options.length - 1; i >= 0; i--) { - var option = this.options[i]; - // find value in collection as [['val','text'], ...] - if (Array.isArray(option) && option[0] == value) { - return option[0]; - // find value in collection as ['val', ...] - } else if (option == value) { - return option; + 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 { From 82ad163f1fdd66377800668e211fce79ec133b7b Mon Sep 17 00:00:00 2001 From: Douglas Waltman II Date: Tue, 9 Feb 2016 10:46:12 -0800 Subject: [PATCH 3/4] Added test for selecting number items for 'text','label' style options --- test/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/index.js b/test/index.js index 70957c4..17b4200 100644 --- a/test/index.js +++ b/test/index.js @@ -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, From 7270f3b610fe39544b29cdb5a60f443ef6a40b71 Mon Sep 17 00:00:00 2001 From: Douglas Waltman II Date: Tue, 9 Feb 2016 12:47:14 -0800 Subject: [PATCH 4/4] Clarified the use of i variable for use in loops only --- ampersand-select-view.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ampersand-select-view.js b/ampersand-select-view.js index b0b2f69..8554eec 100644 --- a/ampersand-select-view.js +++ b/ampersand-select-view.js @@ -351,7 +351,7 @@ 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); @@ -359,18 +359,17 @@ 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)) { - var i = this.options.length - 1; - if (i >= 0) { + if (this.options.length > 0) { // find value in collection as [['val','text'], ...] if (Array.isArray(this.options[0])) { - for (i; i >= 0; i--) { + 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; i >= 0; i--) { + for (i = this.options.length - 1; i >= 0; i--) { if (this.options[i] == value) { return this.options[i]; }