|
51 | 51 | * @param String options[placeholder] Placeholder text or html to insert when element is empty. ** |
52 | 52 | * @param String options[onblur] 'cancel', 'submit', 'ignore' or function ?? |
53 | 53 | * |
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. |
57 | 59 | * |
58 | 60 | */ |
59 | 61 |
|
|
74 | 76 | loadtext : 'Loading...', |
75 | 77 | placeholder: 'Click to edit', |
76 | 78 | loaddata : {}, |
77 | | - submitdata : {} |
| 79 | + submitdata : {}, |
| 80 | + ajaxoptions: {} |
78 | 81 | }; |
79 | 82 |
|
80 | 83 | if(options) { |
|
95 | 98 | var callback = settings.callback || function() { }; |
96 | 99 | var onsubmit = settings.onsubmit || function() { }; |
97 | 100 | var onreset = settings.onreset || function() { }; |
| 101 | + var onerror = settings.onerror || function() { }; |
98 | 102 |
|
99 | 103 | /* add custom event if it does not exist */ |
100 | 104 | if (!$.isFunction($(this)[settings.event])) { |
|
163 | 167 | $(self).html(''); |
164 | 168 |
|
165 | 169 | /* create the form object */ |
166 | | - var form = $('<form/>'); |
| 170 | + var form = $('<form />'); |
167 | 171 |
|
168 | 172 | /* apply css or style or both */ |
169 | 173 | if (settings.cssclass) { |
|
324 | 328 |
|
325 | 329 | /* show the saving indicator */ |
326 | 330 | $(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]); |
334 | 347 | } |
335 | | - }); |
336 | | - } |
337 | | - |
| 348 | + } |
| 349 | + |
| 350 | + /* override with what is given in settings.ajaxoptions */ |
| 351 | + $.extend(ajaxoptions, settings.ajaxoptions); |
| 352 | + $.ajax(ajaxoptions); |
| 353 | + |
| 354 | + } |
338 | 355 | } |
339 | 356 | } |
340 | 357 |
|
|
0 commit comments