Last active
October 7, 2015 17:18
-
-
Save olivernn/3199627 to your computer and use it in GitHub Desktop.
New Davis Hash Routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Davis.hash = function () { | |
var handlers = [], | |
lastPolledLocation | |
var triggerHandlers = function (request) { | |
Davis.utils.forEach(handlers, function (handler) { | |
handler(request) | |
}) | |
} | |
var current = function () { | |
return window.location.hash.replace(/^#?\/?/, '/') | |
} | |
var assign = function (req) { | |
req.fullPath = req.fullPath.split('#')[1] | |
if (req.method === 'get') { | |
window.location.hash = req.location() | |
} else { | |
triggerHandlers(req) | |
} | |
} | |
var onChange = function (handler) { | |
handlers.push(handler) | |
} | |
if ('onhashchange' in window) { | |
Davis.$(window).bind('hashchange', function () { | |
var request = new Davis.Request (current()) | |
triggerHandlers(request) | |
}) | |
} else { | |
setInterval(function () { | |
if (lastPolledLocation != current()) { | |
lastPolledLocation = current() | |
var request = new Davis.Request (lastPolledLocation) | |
triggerHandlers(request) | |
} | |
}, 200) | |
} | |
Davis.supported = function () { | |
return true | |
} | |
Davis.location.setLocationDelegate({ | |
onChange: onChange, | |
current: current, | |
assign: assign, | |
replace: assign | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Davis.extend(Davis.hash) | |
var app = Davis(function () { | |
this.get('/foo', function (req) { | |
// do something cool | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment