all callbacks scope changed to element

This commit is contained in:
vitalets
2012-12-05 12:19:49 +04:00
parent 2809edb3a6
commit 1f51c0726c
3 changed files with 28 additions and 17 deletions

@ -4,6 +4,8 @@ X-editable changelog
Version 1.1.2 wip Version 1.1.2 wip
---------------------------- ----------------------------
[enh] all callbacks scope changed to element (vitalets)
[enh] new option 'cancelnochange' to cancel or submit value when it was not changed in form (vitalets)
[enh] composite pk can be defined as JSON in data-pk attribute (vitalets) [enh] composite pk can be defined as JSON in data-pk attribute (vitalets)
[enh #30] new option 'sourceCache' true|false to disable cache for select (vitalets) [enh #30] new option 'sourceCache' true|false to disable cache for select (vitalets)
[bug #34] inputclass span* broken with fluid bootstrap layout. Classes changed to 'input-*'. (vitalets) [bug #34] inputclass span* broken with fluid bootstrap layout. Classes changed to 'input-*'. (vitalets)

@ -222,12 +222,15 @@ Makes editable any HTML element on the page. Applied as jQuery method.
if(!this.container) { if(!this.container) {
var containerOptions = $.extend({}, this.options, { var containerOptions = $.extend({}, this.options, {
value: this.value, value: this.value,
autohide: false //element will take care to show/hide container autohide: false, //element will take care to show/hide container. Otherwise hide() will be called twice
scope: this.$element[0] //set scope to element
}); });
this.$element.editableContainer(containerOptions); this.$element.editableContainer(containerOptions);
this.$element.on({ this.$element.on({
save: $.proxy(this.save, this), save: $.proxy(this.save, this),
cancel: $.proxy(this.hide, this) cancel: $.proxy(function(){
this.hide();
}, this)
}); });
this.container = this.$element.data('editableContainer'); this.container = this.$element.data('editableContainer');
} else if(this.container.tip().is(':visible')) { } else if(this.container.tip().is(':visible')) {
@ -338,7 +341,7 @@ Makes editable any HTML element on the page. Applied as jQuery method.
if(this.container) { if(this.container) {
this.container.activate(); this.container.activate();
} }
} }
}; };
/* EDITABLE PLUGIN DEFINITION /* EDITABLE PLUGIN DEFINITION
@ -526,7 +529,7 @@ Makes editable any HTML element on the page. Applied as jQuery method.
**/ **/
autotext: 'auto', autotext: 'auto',
/** /**
Wether to return focus on element after form is closed. Whether to return focus on element after form is closed.
This allows fully keyboard input. This allows fully keyboard input.
@property enablefocus @property enablefocus

@ -43,16 +43,16 @@ $(function () {
asyncTest("should load correct value and save new entered text (and value)", function () { asyncTest("should load correct value and save new entered text (and value)", function () {
var v = 'ab<b>"', var v = 'ab<b>"',
esc_v = $('<div>').text(v).html(), esc_v = $('<div>').text(v).html(),
e = $('<a href="#" data-pk="1" data-name="text1" data-url="post-text.php" data-params="{\'q\': \'w\'}">'+esc_v+'</a>').appendTo(fx).editable({ e = $('<a href="#" data-pk="1" data-name="text1" data-url="post-text-main.php" data-params="{\'q\': \'w\'}">'+esc_v+'</a>').appendTo(fx).editable({
success: function(data) { success: function(response, newValue) {
return false; equal(newValue, newText, 'new value in success correct');
} }
}), }),
data, data,
newText = 'cd<e>;"'; newText = 'cd&gt;e>;"';
$.mockjax({ $.mockjax({
url: 'post-text.php', url: 'post-text-main.php',
response: function(settings) { response: function(settings) {
data = settings.data; data = settings.data;
} }
@ -61,13 +61,13 @@ $(function () {
e.click() e.click()
var p = tip(e); var p = tip(e);
ok(p.is(':visible'), 'popover visible') ok(p.is(':visible'), 'popover visible');
ok(p.find('.editableform-loading').length, 'loading class exists') ok(p.find('.editableform-loading').length, 'loading class exists');
ok(!p.find('.editableform-loading').is(':visible'), 'loading class is hidden') ok(!p.find('.editableform-loading').is(':visible'), 'loading class is hidden');
ok(p.find('input[type=text]').length, 'input exists') ok(p.find('input[type=text]').length, 'input exists');
equal(p.find('input[type=text]').val(), v, 'input contain correct value') equal(p.find('input[type=text]').val(), v, 'input contain correct value');
p.find('input').val(newText); p.find('input').val(newText);
p.find('button[type=submit]').click(); p.find('form').submit();
ok(p.find('.editableform-loading').is(':visible'), 'loading class is visible'); ok(p.find('.editableform-loading').is(':visible'), 'loading class is visible');
setTimeout(function() { setTimeout(function() {
@ -88,7 +88,10 @@ $(function () {
asyncTest("should show error on server validation", function () { asyncTest("should show error on server validation", function () {
var msg = 'required', var msg = 'required',
e = $('<a href="#" data-name="text1">abc</a>').appendTo(fx).editable({ e = $('<a href="#" data-name="text1">abc</a>').appendTo(fx).editable({
validate: function(value) { if(value == '') return msg; } validate: function(value) {
ok(this === e[0], 'scope is ok');
if(value == '') return msg;
}
}), }),
newText = ''; newText = '';
@ -153,6 +156,7 @@ $(function () {
var newText = 'cd<e>;"', var newText = 'cd<e>;"',
e = $('<a href="#" data-pk="1" data-url="post.php" data-name="text1">abc</a>').appendTo(fx).editable({ e = $('<a href="#" data-pk="1" data-url="post.php" data-name="text1">abc</a>').appendTo(fx).editable({
success: function(response, newValue) { success: function(response, newValue) {
ok(this === e[0], 'scope is ok');
equal(newValue, newText, 'value in success passed correctly'); equal(newValue, newText, 'value in success passed correctly');
return 'error'; return 'error';
} }
@ -242,6 +246,7 @@ $(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({
name: 'username', name: 'username',
params: function(params) { params: function(params) {
ok(this === e[0], 'scope is ok');
equal(params.pk, 1, 'params in func already have values (pk)'); equal(params.pk, 1, 'params in func already have values (pk)');
return { q: 2, pk: 3 }; return { q: 2, pk: 3 };
}, },
@ -304,11 +309,12 @@ $(function () {
asyncTest("submit to url defined as function", function () { asyncTest("submit to url defined as function", function () {
expect(3); expect(4);
var newText = 'qwe', var newText = 'qwe',
//should be called even without pk! //should be called even without pk!
e = $('<a href="#" data-pk1="1" id="a"></a>').appendTo(fx).editable({ e = $('<a href="#" data-pk1="1" id="a"></a>').appendTo(fx).editable({
url: function(params) { url: function(params) {
ok(this === e[0], 'scope is ok');
ok(params.value, newText, 'new text passed in users function'); ok(params.value, newText, 'new text passed in users function');
var d = new $.Deferred; var d = new $.Deferred;
return d.reject('my error'); return d.reject('my error');