route validation and prevent navigation from breaking on re-cklick
This commit is contained in:
30
js/router.js
30
js/router.js
@@ -2,7 +2,7 @@
|
||||
|
||||
ibuyproperties.router=(function(){
|
||||
var allRoutes=[];
|
||||
|
||||
|
||||
function route() {
|
||||
var hash = window.location.hash;
|
||||
var lastLocation = null;
|
||||
@@ -14,12 +14,18 @@ ibuyproperties.router=(function(){
|
||||
if (lastLocation != hash) {
|
||||
this.lastLocation=hash;
|
||||
|
||||
var routeFoundMainNav=false;
|
||||
var promisesMainNav=[];
|
||||
// Generic routing based on template names
|
||||
$.each(ibuyproperties.templates.getType("mainPages"),function(pos,name){
|
||||
var def = new $.Deferred();
|
||||
promisesMainNav.push(def);
|
||||
|
||||
var re=new RegExp("^#" + name + "[/]*");
|
||||
if (ibuyproperties.isDebugMode) { console.log("Generic Router - test ",name);}
|
||||
// debugger;
|
||||
if (hash.match(re)) {
|
||||
routeFoundMainNav=true;
|
||||
if (ibuyproperties.isDebugMode) { console.log("Generic Router - match ",name);}
|
||||
if ($("#pnl_" +name).length === 0) {
|
||||
$("#content").empty().append(ibuyproperties.templateHelper.get(name)({templateName:name}));
|
||||
@@ -27,30 +33,40 @@ ibuyproperties.router=(function(){
|
||||
ibuyproperties.navShow('mainScreen',name);
|
||||
$('.in,.open').removeClass('in open');
|
||||
}
|
||||
def.resolve();
|
||||
});
|
||||
|
||||
$.when.apply(undefined,promisesMainNav).then(function(){if (!routeFoundMainNav) {console.error("NO matching Navigation tab route found");}});
|
||||
|
||||
var promisesDyn=[];
|
||||
var routeFoundDynamic=false;
|
||||
// Dynamic route handling
|
||||
$.each(allRoutes,function(pos,theRoute){
|
||||
var def = new $.Deferred();
|
||||
promisesDyn.push(def);
|
||||
if (ibuyproperties.isDebugMode) { console.log("Dynamic Router - test ",hash ,theRoute);}
|
||||
|
||||
|
||||
var re=new RegExp(theRoute.re);
|
||||
if (hash.match(re)) {
|
||||
routeFoundDynamic=true;
|
||||
if (ibuyproperties.isDebugMode) { console.log("Dynamic Router - match ",hash,theRoute);}
|
||||
theRoute.cb.call();
|
||||
return false; // exit the loop
|
||||
}
|
||||
def.resolve();
|
||||
});
|
||||
|
||||
$.when.apply(undefined,promisesDyn).then(function(){if (!routeFoundDynamic) {console.error("NO matching Dynamic route found");}});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function addRoute(re, callBack){
|
||||
var theRoute={re:re,cb:callBack};
|
||||
allRoutes.push(theRoute);
|
||||
|
||||
|
||||
}
|
||||
return {
|
||||
route:route,
|
||||
addRoute:addRoute
|
||||
};
|
||||
}());
|
||||
}());
|
||||
|
||||
Reference in New Issue
Block a user