From 9b8375cf2c328cfcb66dae8283cfcacdaeb242c2 Mon Sep 17 00:00:00 2001
From: Bernhard Posselt <nukeawhale@gmail.com>
Date: Wed, 14 Nov 2012 22:37:21 +0100
Subject: [PATCH] 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)) {
-- 
GitLab