diff --git a/CHANGELOG.txt b/CHANGELOG.txt index cf4a8bb..455e181 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -4,6 +4,7 @@ X-editable changelog Version 1.2.1 wip ---------------------------- +[enh #40] 'params' option defined as function overwrites original ajax data instead of appending (vitalets) [bug] datepicker: error when click on arrows after clear date (vitalets) [enh] 'hidden' event: added possible value of reason param - 'nochange'. Occurs when form is submitted but value was not changed (vitalets) [enh] 'submit' method changed: error-callback's parameter simplified (vitalets) diff --git a/src/editable-form/editable-form.js b/src/editable-form/editable-form.js index e0a9a6e..d58ed44 100644 --- a/src/editable-form/editable-form.js +++ b/src/editable-form/editable-form.js @@ -264,7 +264,7 @@ Editableform is linked with one of input types, e.g. 'text', 'select' etc. //additional params if(typeof this.options.params === 'function') { - $.extend(params, this.options.params.call(this.options.scope, params)); + params = this.options.params.call(this.options.scope, params); } else { //try parse json in single quotes (from data-params attribute) this.options.params = $.fn.editableutils.tryParseJson(this.options.params, true); @@ -375,10 +375,13 @@ Editableform is linked with one of input types, e.g. 'text', 'select' etc. **/ url:null, /** - Additional params for submit. Function can be used to calculate params dynamically + Additional params for submit. If defined as <code>object</code> - it is **appended** to original ajax data (pk, name and value). + If defined as <code>function</code> - returned object **overwrites** original ajax data. @example params: function(params) { - return { a: 1 }; + //originally params contain pk, name and value + params.a = 1; + return params; } @property params diff --git a/test/unit/common.js b/test/unit/common.js index d0a5bee..ed95e0f 100644 --- a/test/unit/common.js +++ b/test/unit/common.js @@ -444,6 +444,81 @@ e.remove(); start(); }, timeout); - }); + }); + + + asyncTest("should submit all required params", function () { + var e = $('<a href="#" data-pk="1" data-url="post-resp.php">abc</a>').appendTo(fx).editable({ + name: 'username', + params: { + q: 2 + }, + ajaxOptions: { + dataType: 'json' + }, + success: function(resp) { + equal(resp.dataType, 'json', 'dataType ok'); + equal(resp.data.pk, 1, 'pk ok'); + equal(resp.data.name, 'username', 'name ok'); + equal(resp.data.value, newText, 'value ok'); + equal(resp.data.q, 2, 'additional params ok'); + } + }), + newText = 'cd<e>;"' + + e.click() + var p = tip(e); + + ok(p.find('input[type=text]').length, 'input exists') + p.find('input').val(newText); + p.find('form').submit(); + + setTimeout(function() { + e.remove(); + start(); + }, timeout); + + }); + + asyncTest("params as function", function () { + var e = $('<a href="#" data-pk="1" data-url="post-params-func.php">abc</a>').appendTo(fx).editable({ + name: 'username', + params: function(params) { + ok(this === e[0], 'scope is ok'); + equal(params.pk, 1, 'params in func already have values (pk)'); + return $.extend(params, {q: 2, pk: 3}); + }, + ajaxOptions: { + headers: {"myHeader": "123"} + } + }), + newText = 'cd<e>;"' + + $.mockjax({ + url: 'post-params-func.php', + response: function(settings) { + equal(settings.dataType, undefined, 'dataType undefined (correct)'); + equal(settings.data.pk, 3, 'pk ok'); + equal(settings.data.name, 'username', 'name ok'); + equal(settings.data.value, newText, 'value ok'); + equal(settings.data.q, 2, 'additional params ok'); + } + }); + + + e.click() + var p = tip(e); + + ok(p.find('input[type=text]').length, 'input exists') + p.find('input').val(newText); + p.find('form').submit(); + + setTimeout(function() { + e.remove(); + start(); + }, timeout); + + }); + }(jQuery)); \ No newline at end of file diff --git a/test/unit/text.js b/test/unit/text.js index 7228ac1..e8e81fe 100644 --- a/test/unit/text.js +++ b/test/unit/text.js @@ -211,79 +211,6 @@ $(function () { }); - - asyncTest("should submit all required params", function () { - var e = $('<a href="#" data-pk="1" data-url="post-resp.php">abc</a>').appendTo(fx).editable({ - name: 'username', - params: { - q: 2 - }, - ajaxOptions: { - dataType: 'json' - }, - success: function(resp) { - equal(resp.dataType, 'json', 'dataType ok'); - equal(resp.data.pk, 1, 'pk ok'); - equal(resp.data.name, 'username', 'name ok'); - equal(resp.data.value, newText, 'value ok'); - equal(resp.data.q, 2, 'additional params ok'); - } - }), - newText = 'cd<e>;"' - - e.click() - var p = tip(e); - - ok(p.find('input[type=text]').length, 'input exists') - p.find('input').val(newText); - p.find('form').submit(); - - setTimeout(function() { - e.remove(); - start(); - }, timeout); - - }); - - asyncTest("params as function", function () { - var e = $('<a href="#" data-pk="1" data-url="post-params-func.php">abc</a>').appendTo(fx).editable({ - name: 'username', - params: function(params) { - ok(this === e[0], 'scope is ok'); - equal(params.pk, 1, 'params in func already have values (pk)'); - return { q: 2, pk: 3 }; - }, - ajaxOptions: { - headers: {"myHeader": "123"} - } - }), - newText = 'cd<e>;"' - - $.mockjax({ - url: 'post-params-func.php', - response: function(settings) { - equal(settings.dataType, undefined, 'dataType undefined (correct)'); - equal(settings.data.pk, 3, 'pk ok'); - equal(settings.data.name, 'username', 'name ok'); - equal(settings.data.value, newText, 'value ok'); - equal(settings.data.q, 2, 'additional params ok'); - } - }); - - - e.click() - var p = tip(e); - - ok(p.find('input[type=text]').length, 'input exists') - p.find('input').val(newText); - p.find('form').submit(); - - setTimeout(function() { - e.remove(); - start(); - }, timeout); - - }); asyncTest("ajaxOptions", function () { var e = $('<a href="#" data-pk="1" data-url="post-options.php">abc</a>').appendTo(fx).editable({