Skip to content

Commit

Permalink
Merge pull request #3 from fernandezpablo85/master
Browse files Browse the repository at this point in the history
Use 'hashchange' when possible
  • Loading branch information
matix committed Mar 16, 2012
2 parents ba2013f + c69978d commit 74798e1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
@@ -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" />
Expand Down
45 changes: 41 additions & 4 deletions dist/deeplinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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));
}
},

/**
Expand Down Expand Up @@ -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;
}
};

Expand Down Expand Up @@ -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){
Expand Down
2 changes: 1 addition & 1 deletion dist/deeplinker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/deeplinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
},

/**
Expand Down

0 comments on commit 74798e1

Please sign in to comment.