Skip to content

Instantly share code, notes, and snippets.

@Frogmouth
Last active August 29, 2015 14:00
Show Gist options
  • Save Frogmouth/11397004 to your computer and use it in GitHub Desktop.
Save Frogmouth/11397004 to your computer and use it in GitHub Desktop.
_Route.js - Simple JS #hashtag Routing (BETA)

_Route.js - simple Javascript #hash routing

Simple pseudo class that allows you to attach a specific callback to a specific #hash url. Follow this example to understand

####Getting Started: "Hello word"

var Nav =  new _Route();
Nav.route("/hello/:name", function(name){
    alert("World say Hello, " + name);
});
Nav.listen();

If you load the page with #!/hello/Frogmouth, browser alert this message: "World say Hello, Frogmouth".

Call #!/hello/Frogmouth or #!/hello/Frogmouth*/*** is the same, because "/" is optional (or useless).

You can use also this syntax:

var Nav =  new _Route([
    ["/hello/:name", 
    function(name){
         alert("World say Hello, " + name);
    }]
]);
Nav.listen();

Class _Route

_Route accepts one argument, an array that contain arrays with 2 items, first (0 indexed) is the #hash url without #! (ex. "/hello/:name") the second item is the function that was calling like a route callback.

new _Route(array);

Array's structure:

[
    [
        "/hashPath",
        function(){}
    ],
    [
        "/hashPath/:id",
        function(id){}
    ],
    ...
]

#####The two items are mandatory.

Hash Path:
  1. Hash Path not need to specificate #!
  2. the first character of Hash Path must be "/"
  3. "/" only, is the default behavior (if is instanced, the callback was trigged if not Hash Path was specified, ex. #!/ or #!)
  4. to specificate a dynamic element of the path (like id or string) you can append to variable name ":". An example: #!/player/:id match #!/player/1 or #!/player/duncan
Callback:
  1. the value of this in the callback is the _Route object.
  2. the arguments depends to Hash Path (every :element is an argument of the function)
  3. the name of the arguments aren't necessary the same of the :element, the value of an argument depends of his position, example:

In this Hash Path : /test/:element/:id, the first argument of the callback function is element's value, and the second is id's value.

  1. if the function return false, the routing past to the next Hash Path in the route set, in the other case the route ended.

Enjoy.

#####Sorry for my english but I have't time to correct my stupid error. :)

/*
* @author Frogmouth
* @version 1.4 BETA
*
*/
var _Route = function(route) {
this.debug = true;
this.hash;
this._route = [];
this.log = function() {
if (!this.debug || typeof console === "undefined") return false;
console.log.apply(console, arguments);
}
this.read = function() {
this.hash = location.hash.replace(/#!\//, "");
}
this.go = function() {
location.hash = "#!" + this.hash;
this.listen();
}
this.route = function(h, callback) {
if (typeof h !== "string" || typeof callback !== "function") return this.log("Errore nella istanza di route");
h = h.replace(/\/$/m, "").replace("/", "");
var r = new RegExp(h.replace(/:[^\s\/]+/g, '([\\w-]+)+') + "$", "m");
this._route.push({
reg: r,
c: callback
});
}
this.listen = function(m) {
this.read();
this.log("Number of route:", this._route.length);
for (var x = 0; x < this._route.length; x++) {
m = this.hash.match(this._route[x].reg);
if (m !== null) {
if (!this._route[x].c.apply(this, m.sort(0, 1))) continue;
return;
}
}
}
this.init = function(r) {
if (typeof r === "object") {
for (var x = 0; x < r.length; x++) {
if (typeof r[x] !== "object" && typeof r[x][0] !== "string" && r[x][1] !== "function") continue;
this.log({
reg: r[x][0],
c: r[x][1]
}, "added");
this._route.push({
reg: r[x][0],
c: r[x][1]
});
}
}
}
this.init(route);
}
@Frogmouth
Copy link
Author

This is only a Beta, :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment