success returns object
This commit is contained in:
@ -1,5 +1,10 @@
|
|||||||
X-editable changelog
|
X-editable changelog
|
||||||
=============================
|
=============================
|
||||||
|
|
||||||
|
|
||||||
|
Version 1.1.1 wip
|
||||||
|
----------------------------
|
||||||
|
[enh] success callback can return object to overwrite submitted value (vitalets)
|
||||||
|
|
||||||
|
|
||||||
Version 1.1.0 Nov 27, 2012
|
Version 1.1.0 Nov 27, 2012
|
||||||
|
@ -177,13 +177,20 @@ Editableform is linked with one of input types, e.g. 'text' or 'select'.
|
|||||||
//sending data to server
|
//sending data to server
|
||||||
$.when(this.save(newValueStr))
|
$.when(this.save(newValueStr))
|
||||||
.done($.proxy(function(response) {
|
.done($.proxy(function(response) {
|
||||||
var error;
|
//run success callback
|
||||||
//call success callback. if it returns string --> show error
|
var res = typeof this.options.success === 'function' ? this.options.success.call(this, response, newValue) : null;
|
||||||
if(error = this.options.success.call(this, response, newValue)) {
|
|
||||||
this.error(error);
|
//if it returns string --> show error
|
||||||
|
if(res && typeof res === 'string') {
|
||||||
|
this.error(res);
|
||||||
this.showForm();
|
this.showForm();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//if it returns object like {newValue: <something>} --> use that value
|
||||||
|
if(res && typeof res === 'object' && res.hasOwnProperty('newValue')) {
|
||||||
|
newValue = res.newValue;
|
||||||
|
}
|
||||||
|
|
||||||
//clear error message
|
//clear error message
|
||||||
this.error(false);
|
this.error(false);
|
||||||
@ -398,9 +405,13 @@ Editableform is linked with one of input types, e.g. 'text' or 'select'.
|
|||||||
**/
|
**/
|
||||||
validate: null,
|
validate: null,
|
||||||
/**
|
/**
|
||||||
Success callback. Called when value successfully sent on server and response status = 200.
|
Success callback. Called when value successfully sent on server and **response status = 200**.
|
||||||
Can be used to process json response. If this function returns string - means error occured and string is shown as error message.
|
Usefull to work with json response. For example, if your backend response can be <code>{success: true}</code>
|
||||||
|
or <code>{success: false, msg: "server error"}</code> you can check it inside this callback.
|
||||||
|
If it returns **string** - means error occured and string is shown as error message.
|
||||||
|
If it returns **object like** <code>{newValue: <something>}</code> - it overwrites value, submitted by user.
|
||||||
|
Otherwise newValue simply rendered into element.
|
||||||
|
|
||||||
@property success
|
@property success
|
||||||
@type function
|
@type function
|
||||||
@default null
|
@default null
|
||||||
|
@ -80,8 +80,7 @@ function getAssets(f, c, src, libs) {
|
|||||||
//core
|
//core
|
||||||
js.unshift(bootstrap+'js/bootstrap.js')
|
js.unshift(bootstrap+'js/bootstrap.js')
|
||||||
css.unshift(bootstrap+'css/bootstrap.css');
|
css.unshift(bootstrap+'css/bootstrap.css');
|
||||||
// css.push(bootstrap+'css/bootstrap.css');
|
css.unshift(bootstrap+'css/bootstrap-responsive.css');
|
||||||
//css.unshift(bootstrap+'css/bootstrap-responsive.css');
|
|
||||||
|
|
||||||
//editable
|
//editable
|
||||||
js.push(forms+'editable-form-bootstrap.js');
|
js.push(forms+'editable-form-bootstrap.js');
|
||||||
|
@ -211,7 +211,38 @@ $(function () {
|
|||||||
start();
|
start();
|
||||||
}, timeout);
|
}, timeout);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
asyncTest("should show new value if success callback returns object", function () {
|
||||||
|
var newText = 'cd<e>;"',
|
||||||
|
e = $('<a href="#" data-pk="1" data-url="post.php" data-name="text1">abc</a>').appendTo(fx).editable({
|
||||||
|
success: function(response, newValue) {
|
||||||
|
equal(newValue, newText, 'value in success passed correctly');
|
||||||
|
return {newValue: 'xyz'};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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() {
|
||||||
|
ok(!p.is(':visible'), 'popover closed');
|
||||||
|
equal(p.find('.editable-error-block').text(), '', 'no error msg');
|
||||||
|
equal(e.data('editable').value, 'xyz', 'value ok');
|
||||||
|
equal(e.text(), 'xyz', 'text ok');
|
||||||
|
|
||||||
|
p.find('button[type=button]').click();
|
||||||
|
ok(!p.is(':visible'), 'popover was removed');
|
||||||
|
e.remove();
|
||||||
|
start();
|
||||||
|
}, timeout);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
asyncTest("should submit all required params", 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({
|
var e = $('<a href="#" data-pk="1" data-url="post-resp.php">abc</a>').appendTo(fx).editable({
|
||||||
|
Reference in New Issue
Block a user