Changeset 490 for Dev/trunk/src/client/qed-client/app/Router.js
- Timestamp:
- 03/08/14 22:51:23 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/client/qed-client/app/Router.js
r487 r490 1 1 define([ 2 'dojo/_base/declare', 3 'dojo/hash', 4 'dojo/topic', 5 './Content', 6 './Page', 7 './Path' 8 ],function(declare,hash,topic,Content,Page,Path){ 2 "./Content", 3 "./Page", 4 "./Path", 5 "dojo/_base/declare", 6 "dojo/_base/event", 7 "dojo/_base/lang", 8 "dojo/_base/window", 9 "dojo/hash", 10 "dojo/on", 11 "dojo/topic" 12 ], function(Content, Page, Path, declare, event, lang, window, hash, on, topic) { 9 13 10 14 var Router = declare(null,{ … … 12 16 _routes: null, 13 17 _previousHash: null, 18 _beforePreviousHash: null, 14 19 15 20 constructor: function() { … … 18 23 startup: function() { 19 24 if ( this._started ) { return; } 20 if ( !Content._started ) { 21 Content.startup(); 22 } 25 Content.startup(); 23 26 this._started = true; 24 25 var self = this;26 27 27 if ( hash() === "" ) { 28 28 hash(Path.getDefault()); 29 29 } 30 this._handlePathChange(hash()); 31 topic.subscribe("/dojo/hashchange", function(){ 32 self._handlePathChange.apply(self,arguments); 33 }); 30 this._handleHashChange(hash()); 31 window.onbeforeunload = function() { 32 return Content.isDirty() && "Unsaved changes, leave anyway?"; 33 }; 34 topic.subscribe("/dojo/hashchange", 35 lang.hitch(this,'_handleHashChange')); 34 36 }, 35 37 register: function(route) { … … 68 70 return callback; 69 71 }, 70 _handlePathChange: function(newHash) { 71 if ( this._previousHash === newHash ) { return; } 72 _handleHashChange: function(newHash) { 73 if ( this._previousHash === newHash ) { 74 return false; 75 } 76 if ( Content.isDirty() ) { 77 if ( !confirm("Unsaved changes, leave anyway?") ) { 78 var probablyBack = this._beforePreviousHash === newHash; 79 // if we think we go backwards, we re-add the history 80 // entry, otherwise we reset the current one, 81 // resulting in minor annoyance of double back 82 // behaviour. 83 hash(this._previousHash,!probablyBack); 84 return false; 85 } else { 86 Content.markClean(); 87 } 88 } 89 this._beforePreviousHash = this._previousHash; 72 90 this._previousHash = newHash; 73 91 for (var i = this._routes.length-1; i >= 0; i--) { … … 78 96 route.callback(params); 79 97 } catch(err) { 80 console.error("Page change failed with",err,err. toString());98 console.error("Page change failed with",err,err.stack,err.toString()); 81 99 } 82 return ;100 return true; 83 101 } 84 102 } … … 88 106 console.error("Default page failed.",err); 89 107 } 108 return true; 90 109 }, 91 110 go: function(path,args) { 92 111 if ( !this._started ) { return; } 93 112 var newHash = Path.format(path,args); 94 this._handlePathChange(newHash);95 113 hash(newHash); 96 114 },
Note: See TracChangeset
for help on using the changeset viewer.