From ad87bc464524ee99e2f19eae8c2f8adf570456d9 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 14 Nov 2012 22:37:21 +0100 Subject: [PATCH 1/5] Prevent ajax race conditions when using routes by offering a callback that is run after the the routes have finished loading --- core/js/router.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/core/js/router.js b/core/js/router.js index 8b66f5a05c..02c8d11e69 100644 --- a/core/js/router.js +++ b/core/js/router.js @@ -1,17 +1,30 @@ OC.router_base_url = OC.webroot + '/index.php/', OC.Router = { + loadedCallback: null, + // register your ajax requests to load after the loading of the routes + // has finished. otherwise you face problems with race conditions + registerLoadedCallback: function(callback){ + if(this.routes_request.state() === 'resolved'){ + callback(); + } else { + this.loadedCallback = callback; + } + }, routes_request: $.ajax(OC.router_base_url + 'core/routes.json', { dataType: 'json', success: function(jsondata) { - if (jsondata.status == 'success') { + if (jsondata.status === 'success') { OC.Router.routes = jsondata.data; + if(OC.Router.loadedCallback !== null){ + OC.Router.loadedCallback(); + } } } }), generate:function(name, opt_params) { if (!('routes' in this)) { if(this.routes_request.state() != 'resolved') { - alert('wait');// wait + alert('To avoid race conditions, please register a callback');// wait } } if (!(name in this.routes)) { From bf20021b628e5e00f825203ac37f4878f97a4a2e Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 14 Nov 2012 23:55:37 +0100 Subject: [PATCH 2/5] instead of warning via popup, write to console.warn --- core/js/router.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/js/router.js b/core/js/router.js index 02c8d11e69..406f1912fe 100644 --- a/core/js/router.js +++ b/core/js/router.js @@ -24,7 +24,7 @@ OC.Router = { generate:function(name, opt_params) { if (!('routes' in this)) { if(this.routes_request.state() != 'resolved') { - alert('To avoid race conditions, please register a callback');// wait + console.warn('To avoid race conditions, please register a callback');// wait } } if (!(name in this.routes)) { From e642d18e26f3a6448ac33b2f34341d9b3ee6baae Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Thu, 15 Nov 2012 14:48:06 +0100 Subject: [PATCH 3/5] When using routing in apps, no apps are loaded in the left navigation tree. To fix this: load apps for matching a request --- lib/base.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/base.php b/lib/base.php index c97700b3db..66d2d40028 100644 --- a/lib/base.php +++ b/lib/base.php @@ -488,6 +488,7 @@ class OC{ return; } try { + OC_App::loadApps(); OC::getRouter()->match(OC_Request::getPathInfo()); return; } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { From 25e4a071ef3f89a5573f1d67b0c3cb60ccf7c858 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Thu, 15 Nov 2012 15:01:21 +0100 Subject: [PATCH 4/5] fixed: this.routes_request is a deferred/promise --- core/js/router.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/core/js/router.js b/core/js/router.js index 406f1912fe..77168e3302 100644 --- a/core/js/router.js +++ b/core/js/router.js @@ -4,11 +4,7 @@ OC.Router = { // register your ajax requests to load after the loading of the routes // has finished. otherwise you face problems with race conditions registerLoadedCallback: function(callback){ - if(this.routes_request.state() === 'resolved'){ - callback(); - } else { - this.loadedCallback = callback; - } + this.routes_request.done(callback); }, routes_request: $.ajax(OC.router_base_url + 'core/routes.json', { dataType: 'json', From defdbe3c1036d4eb7b7793d2143a30bc9ce8d778 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Thu, 15 Nov 2012 15:07:01 +0100 Subject: [PATCH 5/5] removed unneeded callback checks in routes_request that could potentially fail --- core/js/router.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/js/router.js b/core/js/router.js index 77168e3302..3562785b34 100644 --- a/core/js/router.js +++ b/core/js/router.js @@ -1,6 +1,5 @@ OC.router_base_url = OC.webroot + '/index.php/', OC.Router = { - loadedCallback: null, // register your ajax requests to load after the loading of the routes // has finished. otherwise you face problems with race conditions registerLoadedCallback: function(callback){ @@ -11,9 +10,6 @@ OC.Router = { success: function(jsondata) { if (jsondata.status === 'success') { OC.Router.routes = jsondata.data; - if(OC.Router.loadedCallback !== null){ - OC.Router.loadedCallback(); - } } } }),