Created
June 30, 2011 04:15
-
-
Save danclien/1055628 to your computer and use it in GitHub Desktop.
JavaScript Router for Dojo
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
/* | |
Now to convince work to let me open-source this... | |
Javascript router for Dojo | |
* Influenced by hij1nx's SugarSkull router, RoR, and the ASP.NET MVC router | |
* Supports static names, regular expressions, and variables | |
* Automatic parsing for specified data types | |
* Events for first arrival (onFirst), entering (onEnter), always (on), and leaving (onLeave) a route | |
* Override-able controllers | |
* Hooks into dojo.hash | |
*/ | |
var router = new Router({ | |
controllers: [ | |
app.controllers.Home, | |
app.controllers.Manage, | |
app.controllers.Users | |
], | |
routeMap: { | |
"static": { //static route | |
}, | |
"{a:int}": { //{variable_name:constraint}, data will be go through parseInt | |
}, | |
"{a:float}": { | |
}, | |
"{a:bool}": { | |
on: "home#index" //calls app.controllers.Home.index(); | |
}, | |
"{a:regexp}": { //regular expression match, returned as string | |
regExp: /abc/ | |
}, | |
"{controller}": { | |
defaultValue: "home", //if the constraint fails, use this value | |
on: "on", | |
onLeave: "leave", | |
onFirst: "first", | |
onEnter: "enter", | |
randomVar: 5, //will be passed to these calls and for child paths | |
"{id:int}": { | |
on: "get" | |
}, | |
"{on}": { | |
defaultValue: "index" | |
} | |
}, | |
"{variable_name:int}": { | |
} | |
} | |
}); | |
router.init(); | |
router.processRoute("/users/5"); //Calls users#first, users#enter, users#on, users#get | |
//dojo.hash("/users/5"); would have done the same thing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment