Permalink
Browse files

Merge pull request #3 from fernandezpablo85/master

Use 'hashchange' when possible
  • Loading branch information...
2 parents ba2013f + c69978d commit 74798e152ded645329636109f5c1fdee1bc28ad6 @matix committed Mar 16, 2012
Showing with 50 additions and 7 deletions.
  1. +1 −1 build.xml
  2. +41 −4 dist/deeplinker.js
  3. +1 −1 dist/deeplinker.min.js
  4. +7 −1 src/deeplinker.js
View
@@ -1,4 +1,4 @@
-<project name="deeplinker" default="all" basedir=".">
+<project name="deeplinker" default="min" basedir=".">
<!-- Setup -->
<property name="SRC_DIR" value="src" description="Source folder" />
<property name="DIST_DIR" value="dist" description="Output folder for build targets" />
View
@@ -64,8 +64,10 @@ window.deeplinker = {
_window: null,
/** Href buffer*/
_href:null,
- /** Hash checking interval call time span */
- _updateRate:100,
+ /** Hash checking interval call default time span */
+ _defaultUpdateRate:100,
+ /** Flag that determines wether deeplinker is paused */
+ _paused: false,
/**
* initializes the deeplinking mechanism.
@@ -74,9 +76,16 @@ window.deeplinker = {
*
* returns: undefined
*/
- init: function(/** DOMWindow */ routingWindow){
+ init: function(/** DOMWindow */ routingWindow, /** Number */ updateRate){
this._window = routingWindow;
- this._checkInterval = setInterval(_checkHash,this._updateRate);
+
+ // Modern browsers can detect hash changes so we don't need to be pulling window.location.href constantly.
+ if ('onhashchange' in this._window) {
+ console.log('onhashchange')
+ this._window.addEventListener('hashchange', _checkHash, false);
+ } else {
+ this._checkInterval = setInterval(_checkHash, (updateRate || this._defaultUpdateRate));
+ }
},
/**
@@ -222,6 +231,33 @@ window.deeplinker = {
}
});
_tree_set(this._routes, pathItems, null);
+ },
+
+ /**
+ * Pauses the hash checking function.
+ *
+ * returns: undefined
+ */
+ pause : function () {
+ this._paused = true;
+ },
+
+ /**
+ * Resumes the hash checking function.
+ *
+ * returns: undefined
+ */
+ resume : function () {
+ this._paused = false;
+ },
+
+ /**
+ * Checks wether deeplinker is paused or not.
+ *
+ * returns: true if paused, false otherwise.
+ */
+ isPaused : function() {
+ return this._paused;
}
};
@@ -290,6 +326,7 @@ function _process_arguments(declaredArgs, declaredPathItems, pathItems ){
//private: constalty check the hash path in the location of the routing window
//given and fire deeplinkig mechanism if registered route found.
function _checkHash(){
+ if (deeplinker._paused) return;
var oldhref = deeplinker._href;
var newhref = deeplinker._window.location.href;
if(oldhref != newhref){
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -25,7 +25,13 @@ window.deeplinker = {
*/
init: function(/** DOMWindow */ routingWindow, /** Number */ updateRate){
this._window = routingWindow;
- this._checkInterval = setInterval(_checkHash, (updateRate || this._defaultUpdateRate));
+
+ // Modern browsers can detect hash changes so we don't need to be pulling window.location.href constantly.
+ if ('onhashchange' in this._window && typeof updateRate === "undefined") {
+ this._window.addEventListener('hashchange', _checkHash, false);
+ } else {
+ this._checkInterval = setInterval(_checkHash, (updateRate || this._defaultUpdateRate));
+ }
},
/**

0 comments on commit 74798e1

Please sign in to comment.