From 9dab8a7b4d3b9dc79ac3179069bbad104cf61dc4 Mon Sep 17 00:00:00 2001 From: Nathan Palmer Date: Sun, 19 Aug 2012 11:43:58 -0400 Subject: [PATCH] Fixed options binder so it would init ops/opsSelected properly for watch callbacks Updated options so you can specify only selectedOptions and it will just pull the values currently in the drop down --- spec/SpineDataBindSpec.js | 2 -- src/spine.databind.coffee | 23 +++++++++++--------- src/spine.databind.js | 46 ++++++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/spec/SpineDataBindSpec.js b/spec/SpineDataBindSpec.js index f69187c..467342a 100644 --- a/spec/SpineDataBindSpec.js +++ b/spec/SpineDataBindSpec.js @@ -562,7 +562,6 @@ describe("Spine.DataBind", function() { Tests(); }); - }); describe("Click", function() { @@ -1252,6 +1251,5 @@ describe("Spine.DataBind", function() { Tests(); }); - }); }); \ No newline at end of file diff --git a/src/spine.databind.coffee b/src/spine.databind.coffee index f42882c..8d0cb73 100644 --- a/src/spine.databind.coffee +++ b/src/spine.databind.coffee @@ -86,12 +86,13 @@ class Options extends Template bind: (operators,model,controller,el,options) -> if options.watch - ops = operators.filter((e) -> e.name is "options")[0] - opsSelected = operators.filter((e) -> e.name is "selectedOptions")[0] + ops = operators.filter((e) -> e.name is "options") + opsSelected = operators.filter((e) -> e.name is "selectedOptions") + together = ops.concat(opsSelected) # ops - @init([ops,opsSelected],model,controller,el,options,"update["+ops.property+"]") - @init([ops,opsSelected],model,controller,el,options,"update["+opsSelected.property+"]") + @init(together,model,controller,el,options,"update["+ops[0].property+"]") if ops and ops.length is 1 + @init(together,model,controller,el,options,"update["+opsSelected[0].property+"]") if opsSelected and opsSelected.length is 1 else @init(operators,model,controller,el,options,"update") @@ -101,13 +102,17 @@ class Options extends Template @update(operators,model,controller,el,options) update: (operators,model,controller,el,options) -> - ops = operators.filter((e) -> e.name is "options")[0] + ops = operators.filter((e) -> e.name is "options") opsSelected = operators.filter((e) -> e.name is "selectedOptions") + selectedOptions = if opsSelected.length is 1 then @get(model,opsSelected[0].property) else [] + selectedOptions = [selectedOptions] if not (selectedOptions instanceof Array) process = (array) -> options = el.children('option') - if array instanceof Array + if not array + result = el.find("option").map((index,item) -> return { text: $(item).text(), value: $(item).val() }) + else if array instanceof Array result = ({ text: item, value: item} for item in array) else result = Object.keys(array) @@ -122,7 +127,7 @@ class Options extends Template ) count = 0 - count = count = count + 1 for own property of result + count = count = count + 1 for own property in result changed = false for item,index in result @@ -155,9 +160,7 @@ class Options extends Template el.trigger("change") if changed - selectedOptions = if opsSelected.length is 1 then @get(model,opsSelected[0].property) else [] - selectedOptions = [selectedOptions] if not (selectedOptions instanceof Array) - array = if ops then @get(model,ops.property,process) or [] else [] + array = if ops.length is 1 then @get(model,ops[0].property,process) else null process(array) if typeof array isnt "function" diff --git a/src/spine.databind.js b/src/spine.databind.js index c47973d..dd53449 100644 --- a/src/spine.databind.js +++ b/src/spine.databind.js @@ -154,17 +154,22 @@ Options.prototype.keys = ["options", "selectedOptions"]; Options.prototype.bind = function(operators, model, controller, el, options) { - var ops, opsSelected, + var ops, opsSelected, together, _this = this; if (options.watch) { ops = operators.filter(function(e) { return e.name === "options"; - })[0]; + }); opsSelected = operators.filter(function(e) { return e.name === "selectedOptions"; - })[0]; - this.init([ops, opsSelected], model, controller, el, options, "update[" + ops.property + "]"); - this.init([ops, opsSelected], model, controller, el, options, "update[" + opsSelected.property + "]"); + }); + together = ops.concat(opsSelected); + if (ops && ops.length === 1) { + this.init(together, model, controller, el, options, "update[" + ops[0].property + "]"); + } + if (opsSelected && opsSelected.length === 1) { + this.init(together, model, controller, el, options, "update[" + opsSelected[0].property + "]"); + } } else { this.init(operators, model, controller, el, options, "update"); } @@ -182,15 +187,26 @@ var array, ops, opsSelected, process, selectedOptions; ops = operators.filter(function(e) { return e.name === "options"; - })[0]; + }); opsSelected = operators.filter(function(e) { return e.name === "selectedOptions"; }); + selectedOptions = opsSelected.length === 1 ? this.get(model, opsSelected[0].property) : []; + if (!(selectedOptions instanceof Array)) { + selectedOptions = [selectedOptions]; + } process = function(array) { - var changed, count, index, item, option, property, result, selected, _i, _j, _len, _ref, + var changed, count, index, item, option, property, result, selected, _i, _j, _k, _len, _len1, _ref, _this = this; options = el.children('option'); - if (array instanceof Array) { + if (!array) { + result = el.find("option").map(function(index, item) { + return { + text: $(item).text(), + value: $(item).val() + }; + }); + } else if (array instanceof Array) { result = (function() { var _i, _len, _results; _results = []; @@ -220,12 +236,12 @@ }); } count = 0; - for (property in result) { - if (!__hasProp.call(result, property)) continue; + for (_i = 0, _len = result.length; _i < _len; _i++) { + property = result[_i]; count = count = count + 1; } changed = false; - for (index = _i = 0, _len = result.length; _i < _len; index = ++_i) { + for (index = _j = 0, _len1 = result.length; _j < _len1; index = ++_j) { item = result[index]; option = options.length > index ? $(options[index]) : null; if ((!isNaN(item.value - 0) && selectedOptions.indexOf(item.value - 0) >= 0) || selectedOptions.indexOf(item.value) >= 0) { @@ -257,7 +273,7 @@ } } if (options.length > count) { - for (index = _j = count, _ref = options.length; count <= _ref ? _j <= _ref : _j >= _ref; index = count <= _ref ? ++_j : --_j) { + for (index = _k = count, _ref = options.length; count <= _ref ? _k <= _ref : _k >= _ref; index = count <= _ref ? ++_k : --_k) { $(options[index]).remove(); changed = true; } @@ -266,11 +282,7 @@ return el.trigger("change"); } }; - selectedOptions = opsSelected.length === 1 ? this.get(model, opsSelected[0].property) : []; - if (!(selectedOptions instanceof Array)) { - selectedOptions = [selectedOptions]; - } - array = ops ? this.get(model, ops.property, process) || [] : []; + array = ops.length === 1 ? this.get(model, ops[0].property, process) : null; if (typeof array !== "function") { return process(array); }