add ajaxOptions param to sumbit method, fixes

This commit is contained in:
vitalets
2012-12-06 22:04:08 +04:00
parent 1b87f99110
commit 01a6788270
3 changed files with 25 additions and 15 deletions

@ -4,6 +4,7 @@ X-editable changelog
Version 1.2.0 wip Version 1.2.0 wip
---------------------------- ----------------------------
[enh #36] 'submit' method: added 'ajaxOptions' property to modify ajax request (vitalets)
[enh] editableContainer removed from docs as not widely used (vitalets) [enh] editableContainer removed from docs as not widely used (vitalets)
[enh] editableContainer: removed 'autohide' option and 'cancel' event. Use 'hidden' event instead (vitalets) [enh] editableContainer: removed 'autohide' option and 'cancel' event. Use 'hidden' event instead (vitalets)
[enh] 'hidden' event: added param 'reason' that points to reason caused hiding (vitalets) [enh] 'hidden' event: added param 'reason' that points to reason caused hiding (vitalets)

@ -429,6 +429,7 @@ Makes editable any HTML element on the page. Applied as jQuery method.
@param {object} options @param {object} options
@param {object} options.url url to submit data @param {object} options.url url to submit data
@param {object} options.data additional data to submit @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.error(obj) error handler (called on both client-side and server-side validation errors)
@param {function} options.success(obj) success handler @param {function} options.success(obj) success handler
@returns {Object} jQuery object @returns {Object} jQuery object
@ -439,21 +440,20 @@ Makes editable any HTML element on the page. Applied as jQuery method.
errors = this.editable('validate'), errors = this.editable('validate'),
values; values;
if(typeof config.error !== 'function') {
config.error = function() {};
}
if($.isEmptyObject(errors)) { if($.isEmptyObject(errors)) {
values = this.editable('getValue'); values = this.editable('getValue');
if(config.data) { if(config.data) {
$.extend(values, config.data); $.extend(values, config.data);
} }
$.ajax({
type: 'POST', $.ajax($.extend({
url: config.url, url: config.url,
data: values, data: values,
type: 'POST',
dataType: 'json' dataType: 'json'
}).success(function(response) { }, config.ajaxOptions))
.success(function(response) {
//successful response
if(typeof response === 'object' && response.id) { if(typeof response === 'object' && response.id) {
$elems.editable('option', 'pk', response.id); $elems.editable('option', 'pk', response.id);
$elems.removeClass('editable-unsaved'); $elems.removeClass('editable-unsaved');
@ -461,13 +461,20 @@ Makes editable any HTML element on the page. Applied as jQuery method.
config.success.apply($elems, arguments); config.success.apply($elems, arguments);
} }
} else { //server-side validation error } else { //server-side validation error
if(typeof config.error === 'function') {
config.error.apply($elems, arguments);
}
}
})
.error(function(){ //ajax error
if(typeof config.error === 'function') {
config.error.apply($elems, arguments); config.error.apply($elems, arguments);
} }
}).error(function(){ //ajax error
config.error.apply($elems, arguments);
}); });
} else { //client-side validation error } else { //client-side validation error
config.error.call($elems, {errors: errors}); if(typeof config.error === 'function') {
config.error.call($elems, {errors: errors});
}
} }
return this; return this;
} }

@ -213,7 +213,6 @@ $(function () {
}); });
asyncTest("'submit' method: client and server validation", function () { asyncTest("'submit' method: client and server validation", function () {
expect(6);
var ev1 = 'ev1', var ev1 = 'ev1',
ev2 = 'ev2', ev2 = 'ev2',
e1v = 'e1v', e1v = 'e1v',
@ -230,6 +229,7 @@ $(function () {
equal(settings.data.text, ev2, 'first value ok'); equal(settings.data.text, ev2, 'first value ok');
equal(settings.data.text1, e1v, 'second value ok'); equal(settings.data.text1, e1v, 'second value ok');
equal(settings.data.a, 123, 'custom data ok'); equal(settings.data.a, 123, 'custom data ok');
equal(settings.type, 'PUT', 'ajaxOptions ok');
this.responseText = {errors: { this.responseText = {errors: {
text1: 'server-invalid' text1: 'server-invalid'
} }
@ -256,11 +256,13 @@ $(function () {
data: {a: 123}, data: {a: 123},
error: function(data) { error: function(data) {
equal(data.errors.text1, 'server-invalid', 'server validation error ok'); equal(data.errors.text1, 'server-invalid', 'server validation error ok');
e.remove(); e.remove();
e1.remove(); e1.remove();
start(); start();
} },
ajaxOptions: {
type: 'PUT'
}
}); });
}); });
@ -339,4 +341,4 @@ $(function () {
equal(e.text(), groups[2], 'new text shown correctly'); equal(e.text(), groups[2], 'new text shown correctly');
}); });
}); });