diff --git a/CHANGELOG.txt b/CHANGELOG.txt index a6434b8..aa870b9 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -4,6 +4,7 @@ X-editable changelog Version 1.4.0 wip ---------------------------- +[enh] select: chnage source via option method, see #61 (vitalets) [bug] select: source loaded twice if sourceCache = false (vitalets) [enh] added `destroy` method, see #61 (vitalets) [enh] textarea: added `rows` property (vitalets) diff --git a/src/editable-form/editable-form.js b/src/editable-form/editable-form.js index 96269a1..d5949ce 100644 --- a/src/editable-form/editable-form.js +++ b/src/editable-form/editable-form.js @@ -311,6 +311,10 @@ Editableform is linked with one of input types, e.g. 'text', 'select' etc. if(key === 'value') { this.setValue(value); } + //pass to input + if(this.input.option) { + this.input.option(key, value); + } }, setValue: function(value, convertStr) { diff --git a/src/element/editable-element.js b/src/element/editable-element.js index 651ef33..c7d6eb1 100644 --- a/src/element/editable-element.js +++ b/src/element/editable-element.js @@ -199,6 +199,11 @@ Makes editable any HTML element on the page. Applied as jQuery method. //transfer new option to container! if(this.container) { this.container.option(key, value); + } else { + //pass option to input directly + if(this.input.option) { + this.input.option(key, value); + } } }, diff --git a/src/inputs/abstract.js b/src/inputs/abstract.js index 7f2d1d4..972c8ee 100644 --- a/src/inputs/abstract.js +++ b/src/inputs/abstract.js @@ -151,11 +151,16 @@ To create your own input you can inherit from this class. this.$input.addClass(this.options.inputclass); } }, + setAttr: function(attr) { if (this.options[attr]) { this.$input.attr(attr, this.options[attr]); } - } + }, + + option: function(key, value) { + this.options[key] = value; + } }; diff --git a/src/inputs/list.js b/src/inputs/list.js index f7a1545..7ecc469 100644 --- a/src/inputs/list.js +++ b/src/inputs/list.js @@ -234,7 +234,17 @@ List - abstract class for inputs that have source option loaded from js array or }); } return result; - } + }, + + option: function(key, value) { + this.options[key] = value; + if(key === 'source') { + this.sourceData = null; + } + if(key === 'prepend') { + this.prependData = null; + } + } }); diff --git a/test/unit/select.js b/test/unit/select.js index 1987a92..92e025a 100644 --- a/test/unit/select.js +++ b/test/unit/select.js @@ -647,6 +647,43 @@ $(function () { }); - + + asyncTest("change source", function () { + var e = $('<a href="#" data-type="select" data-name="load-srv" data-value="2" data-source="groups.php"></a>').appendTo(fx).editable({ + //need to disable cache to force request + sourceCache: false + }); + + setTimeout(function() { + + e.click(); + var p = tip(e); + + equal(p.find('select').find('option').length, size, 'options loaded'); + equal(p.find('select').val(), e.data('editable').value, 'selected value correct') ; + + p.find('.editable-cancel').click(); + ok(!p.is(':visible'), 'popover was closed'); + + $.mockjax({ + url: 'groups1.php', + responseText: {a: 1, 2: 2} + }); + + //set new source + e.editable('option', 'source', 'groups1.php'); + e.click(); + + setTimeout(function() { + ok(p.find('select').length, 'select exists'); + equal(p.find('select').find('option').length, 2, 'new options loaded'); + equal(p.find('select').val(), e.data('editable').value, 'selected value correct') ; + p.find('.editable-cancel').click(); + ok(!p.is(':visible'), 'popover was closed'); + e.remove(); + start(); + }, timeout); + }, timeout); + }); });