diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 099ca9a..6468b29 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -4,7 +4,8 @@ X-editable changelog Version 1.2.1 wip ---------------------------- -[enh] removed default dataType='json' for submit request. Use 'ajaxOptions' to specify dataType if needed (vitalets) +[enh] removed dataType='json' from 'submit' api method. Use 'ajaxOptions' to specify dataType if needed (vitalets) +[enh] removed dataType='json' from saving request. Use 'ajaxOptions' to specify dataType if needed (vitalets) [enh] select: do not show 'sourceError' in element during autotext execution (vitalets) diff --git a/src/element/editable-element.js b/src/element/editable-element.js index 3e723ee..8f29fe0 100644 --- a/src/element/editable-element.js +++ b/src/element/editable-element.js @@ -423,15 +423,15 @@ Makes editable any HTML element on the page. Applied as jQuery method. /** This method collects values from several editable elements and submit them all to server. - It is designed mainly for <a href="#newrecord">creating new records</a>. + Internally it runs client-side validation for all fields and submits only in case of success. @method submit(options) @param {object} options @param {object} options.url url to submit data @param {object} options.data additional data to submit @param {object} options.ajaxOptions additional ajax options - @param {function} options.error(obj) error handler (called on both client-side and server-side validation errors) - @param {function} options.success(obj) success handler + @param {function} options.error(errors) error handler + @param {function} options.success(response, config) success handler. Passing __config__ to be able to call error handler. @returns {Object} jQuery object **/ case 'submit': //collects value, validate and submit to server for creating new record @@ -452,9 +452,9 @@ Makes editable any HTML element on the page. Applied as jQuery method. type: 'POST' }, config.ajaxOptions)) .success(function(response) { - //successful response + //successful response 200 OK if(typeof config.success === 'function') { - config.success.apply($elems, arguments); + config.success.call($elems, response, config); } }) .error(function(){ //ajax error @@ -464,7 +464,7 @@ Makes editable any HTML element on the page. Applied as jQuery method. }); } else { //client-side validation error if(typeof config.error === 'function') { - config.error.call($elems, {errors: errors}); + config.error.call($elems, errors); } } return this; diff --git a/test/unit/api.js b/test/unit/api.js index f38583c..de1146d 100644 --- a/test/unit/api.js +++ b/test/unit/api.js @@ -246,9 +246,8 @@ $(function () { $(fx).find('.new').editable('submit', { url: 'new.php', - error: function(data) { - ok(data.errors, 'errors defined'); - equal(data.errors.text, 'invalid', 'client validation error ok'); + error: function(errors) { + equal(errors.text, 'invalid', 'client validation error ok'); } }); @@ -261,8 +260,19 @@ $(function () { $(fx).find('.new').editable('submit', { url: 'new-error.php', data: {a: 123}, - error: function(data) { - equal(data.errors.text1, 'server-invalid', 'server validation error ok'); + success: function(data, config) { + console.log(data); + ok(data.errors, 'errors received from server'); + ok(typeof config.error === 'function', 'config passed correctly'); + + if(data && data.id) { + //success + } else if(data && data.errors){ + config.error.call(this, data.errors); //call error from success + } + }, + error: function(errors) { + equal(errors.text1, 'server-invalid', 'server validation error ok'); e.remove(); e1.remove(); start();