Changeset 420 for Dev/branches/rest-dojo-ui/client/qed/app
- Timestamp:
- 12/16/12 20:07:35 (12 years ago)
- Location:
- Dev/branches/rest-dojo-ui/client/qed/app
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/branches/rest-dojo-ui/client/qed/app/Notifications.js
r418 r420 3 3 return declare([Toaster],{ 4 4 positionDirection: "br-up", 5 duration: 10005 duration: 3000 6 6 }); 7 7 }); -
Dev/branches/rest-dojo-ui/client/qed/app/Router.js
r410 r420 2 2 'dojo/_base/declare', 3 3 'dojo/hash', 4 'dojo/io-query',5 4 'dojo/topic', 6 5 './Content', 7 './Page' 8 ],function(declare,hash,ioQuery,topic,Content,Page){ 6 './Page', 7 './Path' 8 ],function(declare,hash,topic,Content,Page,Path){ 9 9 10 10 var Router = declare(null,{ … … 12 12 _routes: null, 13 13 _previousHash: null, 14 15 _paramMatch: /:(\w[\w\d]*)/g,16 _paramReplace: "([^\\/!]+)",17 14 18 15 constructor: function() { … … 29 26 30 27 if ( hash() === "" ) { 31 hash( "#!/");28 hash(Path.getDefault()); 32 29 } 33 30 this._handlePathChange(hash()); … … 40 37 console.warn('Registering routes after startup() is called is discouraged.'); 41 38 } 39 var callback = this._normalizeCallback(route); 40 if ( callback ) { 41 if ( route.path ) { 42 route.callback = callback; 43 route.path = new Path(route.path); 44 this._routes.push(route); 45 } else { 46 this._defaultCallback = callback; 47 } 48 } else { 49 console.warn("Route "+(route.path||"default")+" has no action."); 50 } 51 }, 52 _normalizeCallback: function(route) { 42 53 var self = this; 43 var callback ;54 var callback = null; 44 55 if ( route.callback ) { 45 56 callback = function(params){ … … 55 66 }; 56 67 } 57 if ( callback ) { 58 if ( route.path ) { 59 this._routes.push(this._createRoute(route.path,callback)); 60 } else { 61 this._defaultCallback = callback; 62 } 63 } else { 64 console.warn("Route "+(route.path||"default")+" has no action."); 65 } 66 }, 67 _createRoute: function(path,callback) { 68 var match, route = {}; 69 route.callback = callback; 70 route.paramNames = []; 71 while((match = this._paramMatch.exec(path)) !== null){ 72 route.paramNames.push(match[1]); 73 } 74 path = path.replace(this._paramMatch, this._paramReplace); 75 route.regexp = new RegExp('^!'+path+'(!(.*))?$'); 76 return route; 68 return callback; 77 69 }, 78 70 _handlePathChange: function(newHash) { 79 71 if ( this._previousHash === newHash ) { return; } 80 72 this._previousHash = newHash; 81 82 73 for (var i = this._routes.length-1; i >= 0; i--) { 83 var result;84 74 var route = this._routes[i]; 85 86 if ((result = route.regexp.exec(newHash)) !== null) { 87 var numParams = route.paramNames.length; 88 89 var params = {}; 90 for (var j = 0; j < numParams; j++) { 91 params[route.paramNames[j]] = result[j+1]; 92 } 93 94 if ( result.length > numParams+1 && result[numParams+2] ) { 95 params.options = ioQuery.queryToObject(result[numParams+2]); 96 } 97 75 var params = null; 76 if ((params = route.path.match(newHash)) !== null) { 98 77 try { 99 78 route.callback(params); 100 79 } catch(err) { 101 console. warn("Page change failed.",err);80 console.error("Page change failed.",err); 102 81 } 103 82 return; 104 83 } 105 84 } 106 107 85 try { 108 86 this._defaultCallback(); 109 87 } catch(err) { 110 console. warn("Default page failed.",err);88 console.error("Default page failed.",err); 111 89 } 112 90 }, 113 91 go: function(path,args) { 114 92 if ( !this._started ) { return; } 115 var newHash = this._pathToHash(path,args);93 var newHash = Path.format(path,args); 116 94 this._handlePathChange(newHash); 117 95 hash(newHash); 118 96 }, 119 _pathToHash: function(path,args) {120 var hash = '!'+path;121 if ( args ) {122 hash += '!'+ioQuery.objectToQuery(args);123 }124 return hash;125 },126 97 _defaultCallback: function() { 127 98 Content.set(new Page({ 128 templateString: "<div>Requested page not found. Go <a href=\"# !/\">home</a>.</div>"99 templateString: "<div>Requested page not found. Go <a href=\"#"+Path.getDefault()+"\">home</a>.</div>" 129 100 })); 130 101 }
Note: See TracChangeset
for help on using the changeset viewer.