mooRouter v1.0

This plugin provides methods for routing client-side pages and connecting them to actions and events.



Details

Author
Adrian Statescu
Current version
v1.0
GitHub
thinkphp/router.js
Downloads
2891
Category
Utilities
Tags
Report
GitHub Issues

Releases


Dependencies

  • core/1.4.4: *

How to use

First at all, you must create a set of rules that represents the patterns for url matching.

Get started by creating a set of rules:

           var rules = [
           ["/:user/forge/:action/",
           function(data) {       
                 console.log("My Profile",data.user);
           }],
           ["/forge/:action/",
           function(data) {
                 console.log("Add a new plugin",data.action);
           }],
           ["/:user/settings/:action/",
           function(data) {
                 console.loglog("Edit your profile",data.user);
           }],
           ["/logout/",
           function(data) {
                 console.log("Logout",data);
           }]
           ]; 

:key acts as placeholders resulting in matching properties on the data object.

Create, simply, a new object Router.

           var router = new Router(rules);

Be sure to call the method "match". This method iterates over the stored rules and if a match is found, the corresponding handler is fired with a single argument, an object containing properties matching the variable placeholders in the matching rule, otherwise returns undefined.

           window.onhashchange = function() {
                   router.match(location.hash);
           }

Discuss

A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.

Found a bug in this plugin? Please report it this repository's Github Issues.

blog comments powered by Disqus