params can be a func, fixes

This commit is contained in:
vitalets 2012-11-21 14:51:13 +04:00
parent d9e818d287
commit 6a8cb83cbd
4 changed files with 56 additions and 9 deletions

@ -4,6 +4,7 @@ X-editable changelog
Version 1.0.1 wip Version 1.0.1 wip
---------------------------- ----------------------------
[enh #1] params can be a function to calc it dynamically (vitalets)
[enh #6] do not preventDetault() in click when toggle='manual'. This allows to have clickable links (vitalets) [enh #6] do not preventDetault() in click when toggle='manual'. This allows to have clickable links (vitalets)
[bug #3] should not mark element with unsave css if url is user's function (vitalets) [bug #3] should not mark element with unsave css if url is user's function (vitalets)

@ -186,15 +186,22 @@ Editableform is linked with one of input types, e.g. 'text' or 'select'.
if (send) { //send to server if (send) { //send to server
this.showLoading(); this.showLoading();
//try parse json in single quotes //standart params
this.options.params = $.fn.editableform.utils.tryParseJson(this.options.params, true); params = {
params = $.extend({}, this.options.params, {
name: this.options.name || '', name: this.options.name || '',
value: value, value: value,
pk: pk pk: pk
}); };
//additional params
if(typeof this.options.params === 'function') {
$.extend(params, this.options.params.call(this, params));
} else {
//try parse json in single quotes (from data-params attribute)
this.options.params = $.fn.editableform.utils.tryParseJson(this.options.params, true);
$.extend(params, this.options.params);
}
if(typeof this.options.url === 'function') { //user's function if(typeof this.options.url === 'function') { //user's function
return this.options.url.call(this, params); return this.options.url.call(this, params);
@ -289,10 +296,14 @@ Editableform is linked with one of input types, e.g. 'text' or 'select'.
**/ **/
url:null, url:null,
/** /**
Additional params for submit Additional params for submit. Function can be used to calculate params dynamically
@example
params: function() {
return { a: 1 };
}
@property params @property params
@type object @type object|function
@default null @default null
**/ **/
params:null, params:null,
@ -305,7 +316,8 @@ Editableform is linked with one of input types, e.g. 'text' or 'select'.
**/ **/
name: null, name: null,
/** /**
Primary key of editable object (e.g. record id in database). Use Object for composite keys. Primary key of editable object (e.g. record id in database). For composite keys use object, e.g. <code>{id: 1, lang: 'en'}</code>.
Can be calculated dinamically via function.
@property pk @property pk
@type string|object|function @type string|object|function

@ -37,6 +37,7 @@ $(function () {
$.fn.editable.defaults.name = 'name1'; $.fn.editable.defaults.name = 'name1';
//clear cache //clear cache
$(document).removeData('groups.php-'+$.fn.editable.defaults.name); $(document).removeData('groups.php-'+$.fn.editable.defaults.name);
$.support.transition = false;
} }
}); });

@ -4,6 +4,7 @@ $(function () {
setup: function() { setup: function() {
fx = $('#async-fixture'); fx = $('#async-fixture');
$.fn.editable.defaults.name = 'name1'; $.fn.editable.defaults.name = 'name1';
$.support.transition = false;
} }
}); });
@ -242,6 +243,38 @@ $(function () {
}); });
asyncTest("params as function", function () {
var e = $('<a href="#" data-pk="1" data-url="post-resp.php">abc</a>').appendTo(fx).editable({
name: 'username',
params: function(params) {
equal(params.pk, 1, 'params in func already have values (pk)');
return { q: 2, pk: 3 };
},
success: function(resp) {
equal(resp.dataType, 'json', 'dataType ok');
equal(resp.data.pk, 3, 'pk ok');
equal(resp.data.name, 'username', 'name ok');
equal(resp.data.value, newText, 'value ok');
equal(resp.data.q, 2, 'additional params ok');
}
}),
newText = 'cd<e>;"'
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() {
e.remove();
start();
}, timeout);
});
asyncTest("submit to url defined as function", function () { asyncTest("submit to url defined as function", function () {
expect(3); expect(3);
var newText = 'qwe', var newText = 'qwe',