Skip to content

Commit 76abe1f

Browse files
committed
Use $.ajax instead of $.post. Implement onerror hook.
Using $.ajax gives you full access to ajax request configuration.
1 parent 1799323 commit 76abe1f

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

jquery.jeditable.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@
5151
* @param String options[placeholder] Placeholder text or html to insert when element is empty. **
5252
* @param String options[onblur] 'cancel', 'submit', 'ignore' or function ??
5353
*
54-
* TODO @param Function options[onsubmit]
55-
* TODO @param Function options[onreset]
56-
* TODO @param Function options[onerror]
54+
* @param Function options[onsubmit] function(settings, original) { ... } called before submit
55+
* @param Function options[onreset] function(settings, original) { ... } called before reset
56+
* @param Function options[onerror] function(reason, settings, original) { ... } called on error
57+
*
58+
* @param Hash options[ajaxoption] jQuery Ajax options. See docs.jquery.com.
5759
*
5860
*/
5961

@@ -74,7 +76,8 @@
7476
loadtext : 'Loading...',
7577
placeholder: 'Click to edit',
7678
loaddata : {},
77-
submitdata : {}
79+
submitdata : {},
80+
ajaxoptions: {}
7881
};
7982

8083
if(options) {
@@ -95,6 +98,7 @@
9598
var callback = settings.callback || function() { };
9699
var onsubmit = settings.onsubmit || function() { };
97100
var onreset = settings.onreset || function() { };
101+
var onerror = settings.onerror || function() { };
98102

99103
/* add custom event if it does not exist */
100104
if (!$.isFunction($(this)[settings.event])) {
@@ -163,7 +167,7 @@
163167
$(self).html('');
164168

165169
/* create the form object */
166-
var form = $('<form/>');
170+
var form = $('<form />');
167171

168172
/* apply css or style or both */
169173
if (settings.cssclass) {
@@ -324,17 +328,30 @@
324328

325329
/* show the saving indicator */
326330
$(self).html(settings.indicator);
327-
$.post(settings.target, submitdata, function(str) {
328-
$(self).html(str);
329-
self.editing = false;
330-
callback.apply(self, [self.innerHTML, settings]);
331-
/* TODO: this is not dry */
332-
if (!$.trim($(self).html())) {
333-
$(self).html(settings.placeholder);
331+
332+
/* defaults for ajaxoptions */
333+
var ajaxoptions = {
334+
type : 'POST',
335+
data : submitdata,
336+
url : settings.target,
337+
success : function(result, status) {
338+
$(self).html(result);
339+
self.editing = false;
340+
callback.apply(self, [self.innerHTML, settings]);
341+
if (!$.trim($(self).html())) {
342+
$(self).html(settings.placeholder);
343+
}
344+
},
345+
error : function(xhr, status, error) {
346+
onerror.apply(form, [xhr, settings, self]);
334347
}
335-
});
336-
}
337-
348+
}
349+
350+
/* override with what is given in settings.ajaxoptions */
351+
$.extend(ajaxoptions, settings.ajaxoptions);
352+
$.ajax(ajaxoptions);
353+
354+
}
338355
}
339356
}
340357

0 commit comments

Comments
 (0)