Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
initial commit of routerlite
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Grabanski committed Aug 9, 2012
1 parent 754d7fa commit c63b117
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 3 deletions.
52 changes: 49 additions & 3 deletions README.md
@@ -1,4 +1,50 @@
jquery-mobile-routerlite
========================
jquery-mobile-router-lite
=========================
A lightweight and simple router for jQuery Mobile.

A lightweight and simple router for jQuery Mobile.
Why
=====================
**routerlite** was created to give you exactly the power you need to handle routes properly in jQuery Mobile with a very straightforward API. **routerlite** just gives you four simple methods: **routeinit**, **routechange**, **pageinit** and **pagechange**. You can bind a callback when a route or page is first visited, or every time a certain route or page is visited.

There are a lot of different types of page events in jQuery Mobile and sometimes it's hard to figure out which one to bind to and also how to get the current page context when that event fires. So **routerlite** helps you with normalizing event data so you don't have to do any guesswork. **routerlite** normalizes this for you and gives you the current page and path context every time you handle a route or page change.

API Methods
=====================
**$.mobile.routerlite** has four methods:

* **routeinit** - only fires once when the route is first visited
```javascript
// When "/admin" is first visited
$.mobile.routerlite.routeinit("/admin", function(page, path){
doSomething();
});
```

* **routechange** - fires each time the route is visited
```javascript
// For every page in "/admin/*"
$.mobile.routerlite.routechange("/admin", function(page, path){
doSomething();
});
```
* **pageinit** - only fires once when the page is first visited
```javascript
// if we see page with id="checkout" then run this code
$.mobile.routerlite.pageinit("#checkout", function(page){
doSomething();
});
```

* **agechange **- fires each time the page is visited
```javascript
// every time we visit the page with id="checkout" then run this code
$.mobile.routerlite.pageinit("#checkout", function(page){
doSomething();
});
```

Each callback receives two parameters:

* **page** - currently active jQuery mobile page
* **path** - url path of the current route
100 changes: 100 additions & 0 deletions jquery.mobile.routerlite.js
@@ -0,0 +1,100 @@
/*!
* jQuery Mobile Router Lite
* A lightweight and simple router for jQuery Mobile
* https://github.com/1Marc/jquery-mobile-router-lite
*
* Copyright 2012 (c) Marc Grabanski MIT license
*/

(function($){

/*
* $.mobile.routerlite has four methods:
* routeinit - only fires once when the route is first visited
* routechange - fires each time the route is visited
* pageinit - only fires once when the page is first visited
* pagechange - fires each time the page is visited
*
* Each callback receives two parameters:
* page - currently active jQuery mobile page
* path - url path of the current route
*/
$.mobile.routerlite = {
_initroutes: [],
_changeroutes: [],
_getPath: function() {
return window.location.pathname;
},
_trigger: function(callback, uiPage) {
callback(uiPage, this._getPath());
},
_routesInit: function(url, uiPage) {
var self = this;
for (var i = 0; i < this._initroutes.length; i++) {
var route = this._initroutes[i];
if (!route.loaded && url.indexOf(route.pathname) > -1) {
self._trigger(route.callback, uiPage);
route.loaded = true;
}
}
},
_routesChange: function(url, uiPage) {
var self = this;
for (var i = 0; i < this._changeroutes.length; i++) {
var route = this._changeroutes[i];
if (url.indexOf(route.pathname) > -1) {
self._trigger(route.callback, uiPage);
}
}
},
// the first time we visit a route, call teh callback
routeinit: function(route, callback) {
var self = this;
this._initroutes.push({"pathname":route, "callback":callback});
if (!this._initroutescheck) {
$(document).on("pageshow", function(e){
self._routesInit(self._getPath(), e.target);
});
this._initroutescheck = true;
}
},
// when we visit a route, call the callback
routechange: function(route, callback) {
var self = this;
this._changeroutes.push({"pathname":route, "callback":callback});
if (!this._changeroutescheck) {
$(document).on("pagechange", function(e, data){
self._routesChange(self._getPath(), data.toPage);
});
this._changeroutescheck = true;
}
},
// when page has been initialized, call the callback
pageinit: function(pageSelector, callback) {
var self = this;
$(document).on("pageshow", function(e) {
var $page = $(pageSelector);
if ($page.length && !$page.data('init')) {
self._trigger(callback, $page.get(0));
$page.data('init', true);
}
});
},
// when we change to the page that matches the selector, call the callback
pagechange: function(pageSelector, callback) {
var self = this;
$(document).on("pagebeforechange", function(e, data) {
// don't want to trigger on a pure url
if (typeof data.toPage === "string") {
return;
}
if ($(data.toPage).is(pageSelector)) {
$(document).one('pagechange', function(e, data){
self._trigger(callback, data.toPage.get(0));
});
}
});
}
};

})(jQuery);

0 comments on commit c63b117

Please sign in to comment.