sourceCache = false or source as function now trigger request on each show, fix , fix# 254

This commit is contained in:
vitalets 2013-06-13 08:51:58 +04:00
parent f8d8799fbc
commit 7daf7f09bc
5 changed files with 68 additions and 54 deletions

@ -3,6 +3,7 @@ X-editable changelog
Version 1.4.5 wip Version 1.4.5 wip
---------------------------- ----------------------------
[enh #218] sourceCache now disables cache totally (vitalets)
[bug #156] conflict of bootstrap datepicker and jQuery UI datepicker (vitalets) [bug #156] conflict of bootstrap datepicker and jQuery UI datepicker (vitalets)
[enh] update bootstrap-datepicker to 1.1.2 (vitalets) [enh] update bootstrap-datepicker to 1.1.2 (vitalets)
[enh] allow follow links in disabled state (vitalets) [enh] allow follow links in disabled state (vitalets)

@ -181,8 +181,8 @@ Applied as jQuery method.
/* /*
Currently, form is re-rendered on every show. Currently, form is re-rendered on every show.
The main reason is that we dont know, what container will do with content when closed: The main reason is that we dont know, what will container do with content when closed:
remove(), detach() or just hide() - it depend on container. remove(), detach() or just hide() - it depends on container.
Detaching form itself before hide and re-insert before show is good solution, Detaching form itself before hide and re-insert before show is good solution,
but visually it looks ugly --> container changes size before hide. but visually it looks ugly --> container changes size before hide.

@ -59,7 +59,7 @@
*/ */
/*jshint laxcomma: true*/ /*jshint laxcomma: true*/
setPosition: function () { setPosition: function () {
(function() { (function() {
var $tip = this.tip() var $tip = this.tip()
, inside , inside

@ -58,30 +58,32 @@ List - abstract class for inputs that have source option loaded from js array or
// ------------- additional functions ------------ // ------------- additional functions ------------
onSourceReady: function (success, error) { onSourceReady: function (success, error) {
//run source if it function
var source;
if ($.isFunction(this.options.source)) {
source = this.options.source.call(this.options.scope);
this.sourceData = null;
} else {
source = this.options.source;
}
//if allready loaded just call success //if allready loaded just call success
if($.isArray(this.sourceData)) { if(this.options.sourceCache && $.isArray(this.sourceData)) {
success.call(this); success.call(this);
return; return;
} }
// try parse json in single quotes (for double quotes jquery does automatically) //try parse json in single quotes (for double quotes jquery does automatically)
try { try {
this.options.source = $.fn.editableutils.tryParseJson(this.options.source, false); source = $.fn.editableutils.tryParseJson(source, false);
} catch (e) { } catch (e) {
error.call(this); error.call(this);
return; return;
} }
var source = this.options.source;
//run source if it function
if ($.isFunction(source)) {
source = source.call(this.options.scope);
}
//loading from url //loading from url
if (typeof source === 'string') { if (typeof source === 'string') {
//try to get from cache //try to get sourceData from cache
if(this.options.sourceCache) { if(this.options.sourceCache) {
var cacheID = source, var cacheID = source,
cache; cache;

@ -41,13 +41,15 @@ $(function () {
//open second time: items should not dublicate //open second time: items should not dublicate
e.click(); e.click();
p = tip(e);
ok(p.find('select').length, 'select exists'); setTimeout(function() {
equal(p.find('select').find('option').length, size, 'options loaded'); ok(p.find('select').length, 'select exists');
equal(p.find('select').val(), e.data('editable').value, 'selected value correct') ; equal(p.find('select').find('option').length, size, 'options loaded');
equal(p.find('select').val(), e.data('editable').value, 'selected value correct') ;
e.remove();
start(); e.remove();
start();
}, timeout);
}, timeout); }, timeout);
}); });
@ -106,7 +108,8 @@ $(function () {
}); });
test("load options from function returning array", function () { test("load options from function returning array", function () {
var e = $('<a href="#" data-type="select" data-value="2" data-url="post.php">customer</a>').appendTo('#qunit-fixture').editable({ var counter = 0,
e = $('<a href="#" data-type="select" data-value="2" data-url="post.php">customer</a>').appendTo('#qunit-fixture').editable({
pk: 1, pk: 1,
prepend: function() { prepend: function() {
equal(this, e[0], 'prepend scope is element'); equal(this, e[0], 'prepend scope is element');
@ -114,22 +117,30 @@ $(function () {
}, },
source: function() { source: function() {
equal(this, e[0], 'source scope is element'); equal(this, e[0], 'source scope is element');
return groups; return counter ? groupsArr.concat([{10: 'test'}]) : groupsArr;
} }
}); });
e.click() function t() {
var p = tip(e); e.click()
ok(p.is(':visible'), 'popover visible'); var p = tip(e);
ok(p.find('select').length, 'select exists'); ok(p.is(':visible'), 'popover visible');
equal(p.find('select').find('option').length, size+1, 'options loaded'); ok(p.find('select').length, 'select exists');
equal(p.find('select').val(), e.data('editable').value, 'selected value correct') ; equal(p.find('select').find('option').length, size+1+counter, 'options loaded');
p.find('.editable-cancel').click(); equal(p.find('select').val(), e.data('editable').value, 'selected value correct') ;
ok(!p.is(':visible'), 'popover was removed'); p.find('.editable-cancel').click();
ok(!p.is(':visible'), 'popover was removed');
}
//first run
t();
//second time
counter = 1;
t();
}); });
asyncTest("load options from function returning URL", function () { asyncTest("load options from function returning URL", function () {
var e = $('<a href="#" data-type="select" data-value="2" data-url="post.php">customer</a>').appendTo('#qunit-fixture').editable({ var e = $('<a href="#" data-type="select" data-value="2" data-url="post.php">customer</a>').appendTo(fx).editable({
pk: 1, pk: 1,
//need to disable cache to force request //need to disable cache to force request
sourceCache: false, sourceCache: false,
@ -152,13 +163,15 @@ $(function () {
//open second time: items should not dublicate //open second time: items should not dublicate
e.click(); e.click();
p = tip(e);
ok(p.find('select').length, 'select exists'); setTimeout(function() {
equal(p.find('select').find('option').length, size, 'options loaded'); ok(p.find('select').length, 'select exists');
equal(p.find('select').val(), e.data('editable').value, 'selected value correct') ; equal(p.find('select option').length, size, 'options loaded');
equal(p.find('select').val(), e.data('editable').value, 'selected value correct') ;
e.remove();
start(); e.remove();
start();
}, timeout);
}, timeout); }, timeout);
}); });
@ -428,7 +441,7 @@ $(function () {
}); });
//since 1.4.5 sourceCache is more strong: it forces load sourceData on every click
asyncTest("sourceCache: false", function () { asyncTest("sourceCache: false", function () {
$.mockjax({ $.mockjax({
@ -448,6 +461,7 @@ $(function () {
}); });
setTimeout(function() { setTimeout(function() {
//autotext for first only!
equal(req, 1, 'autotext request performed'); equal(req, 1, 'autotext request performed');
//click first //click first
@ -459,7 +473,7 @@ $(function () {
ok(p.is(':visible'), 'popover visible'); ok(p.is(':visible'), 'popover visible');
equal(p.find('select').find('option').length, size, 'options loaded'); equal(p.find('select').find('option').length, size, 'options loaded');
equal(req, 1, 'no additional request performed, loaded on autotext'); equal(req, 2, 'additional request performed (allthough loaded on autotext)');
p.find('.editable-cancel').click(); p.find('.editable-cancel').click();
ok(!p.is(':visible'), 'popover was removed'); ok(!p.is(':visible'), 'popover was removed');
@ -471,7 +485,7 @@ $(function () {
setTimeout(function() { setTimeout(function() {
ok(p.is(':visible'), 'popover2 visible'); ok(p.is(':visible'), 'popover2 visible');
equal(p.find('select').find('option').length, size, 'options loaded'); equal(p.find('select').find('option').length, size, 'options loaded');
equal(req, 2, 'second request performed'); equal(req, 3, 'second request performed');
p.find('.editable-cancel').click(); p.find('.editable-cancel').click();
ok(!p.is(':visible'), 'popover was removed'); ok(!p.is(':visible'), 'popover was removed');
@ -690,16 +704,14 @@ $(function () {
asyncTest("change source", function () { asyncTest("change source", function () {
var e = $('<a href="#" data-type="select" data-name="load-srv" data-value="2" data-source="groups.php"></a>').appendTo(fx).editable({ var e = $('<a href="#" data-type="select" data-name="load-srv" data-value="2" data-source="groups.php">customer</a>').appendTo(fx).editable({
//need to disable cache to force request sourceCache: true
sourceCache: false
}); });
e.click();
var p = tip(e);
setTimeout(function() { setTimeout(function() {
e.click();
var p = tip(e);
equal(p.find('select').find('option').length, size, 'options loaded'); equal(p.find('select').find('option').length, size, 'options loaded');
equal(p.find('select').val(), e.data('editable').value, 'selected value correct') ; equal(p.find('select').val(), e.data('editable').value, 'selected value correct') ;
@ -707,12 +719,12 @@ $(function () {
ok(!p.is(':visible'), 'popover was closed'); ok(!p.is(':visible'), 'popover was closed');
$.mockjax({ $.mockjax({
url: 'groups1.php', url: 'groups-changed.php',
responseText: [{value: 'a', text: 1}, {value: 'b', text: 2}] responseText: [{value: 'a', text: 1}, {value: 'b', text: 2}]
}); });
//set new source //set new source
e.editable('option', 'source', 'groups1.php'); e.editable('option', 'source', 'groups-changed.php');
e.click(); e.click();
setTimeout(function() { setTimeout(function() {
@ -728,8 +740,7 @@ $(function () {
e.remove(); e.remove();
start(); start();
}, timeout); }, timeout);
}, timeout);
}, timeout);
}); });
asyncTest("optgroup", function () { asyncTest("optgroup", function () {