Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:azicchetti/jquerymobile-router

  • Loading branch information...
commit f681852317d7a9181b500091664cc1ae5ba021ec 2 parents e5b48a3 + 4329e10
@azicchetti authored
View
42 README.md
@@ -221,6 +221,48 @@ Syntax 2:
]
```
+Choosing the right event
+-----------------
+In order to successfully exploit routing under jQuery Mobile, the developer should have
+at least a minimal knowledge of its event system (among other things!):
+http://jquerymobile.com/demos/1.0.1/docs/api/events.html
+
+Once you're familiar with page change events, you can choose the right one in order to achieve
+the desired behaviour in your application.
+I know this may be a difficult choice, at least initially, so I'll try to sort things out for you.
+Bear in mind that the following are just suggestions that cannot replace a deep knowledge
+of the jQuery Mobile framework.
+
+For single-file multipage applications (you have an html file containing a lot of jQM pages):
+
+ * pagebeforeshow or pageshow:
+ These events are called every time a particular page is shown.
+ This is perfect to render and update a page, often using the parameters in the hash part of the url
+
+ * pagebeforehide or pagehide:
+ These events are called every time a particular page is hidden.
+ You can use these events to clean views or models, to free resources and clean the DOM.
+
+For ajax applications (multiple files containing a single jQM page):
+
+ * You can still use the events described above... but keep on reading, you may find a better option
+
+ * pagebeforecreate, pagecreate or pageinit:
+ These events are called before/after a jQuery Mobile page has been initialized. Remember that initialization
+ happens ***just once*** for a particular page (unless it's removed from the DOM, see below).
+ If you make changes to the DOM before a page or a widget is initialized, you won't have to update
+ or refresh it. But please note that if your models need to be refreshed through an ajax call,
+ you'll probably get your results back from the remote server when the page has already been widget-ified
+ by jQuery Mobile, so pay attention to this scenario and prefer the "pageinit" event, unless you know
+ what you're doing.
+
+ * pageremove:
+ This one is called when a page is automatically removed from the DOM by the framework. This applies only
+ to pages that were fetched via ajax.
+ When the user navigates away from a certain page, jQuery Mobile will remove it from the DOM to keep its
+ size small, unless otherwise specified (see the data-dom-cache="true" option in the doc).
+ Use this event to clean models and views references.
+
myHandlers object
-----------------
View
7 js/jquery.mobile.router.js
@@ -74,6 +74,9 @@ $(document).bind("mobileinit",function(){
if (config.fixFirstPageDataUrl){
$(document).ready(function(){
+ if (!window.location.pathname.match("/$")){
+ return;
+ }
var page=$(":jqmData(role='page')").first();
var dataUrl=page.jqmData("url"),
guessedDataUrl=window.location.pathname
@@ -81,9 +84,7 @@ $(document).bind("mobileinit",function(){
+window.location.search
+window.location.hash
;
- if (!window.location.pathname.match("/$")){
- return;
- }
+
if (dataUrl!=guessedDataUrl){
page.attr("data-url",guessedDataUrl)
.jqmData("url",guessedDataUrl);
View
2  js/jquery.mobile.router.min.js
@@ -7,4 +7,4 @@
* http://github.com/azicchetti/jquerymobile-router/blob/master/MIT-LICENSE.txt
* http://github.com/azicchetti/jquerymobile-router/blob/master/GPL-LICENSE.txt
*/
-(function(a){a(document).bind("mobileinit",function(){var d=a.extend({fixFirstPageDataUrl:false,firstPageDataUrl:"index.html",ajaxApp:false},a.mobile.jqmRouter||{});var c=true;function b(g){if(c){console.log(g)}}var e=null,f=null;a(document).bind("pagebeforechange",function(j,i){if(typeof i.toPage==="string"){var g=a.mobile.path.parseUrl(i.toPage);e=f;f=g;if(g.hash.indexOf("?")!==-1){var h=g.hash.replace(/\?.*$/,"");i.options.dataUrl=g.href;if(a.mobile.activePage&&h.replace(/^#/,"")==a.mobile.activePage.jqmData("url")){i.options.allowSamePageTransition=true;a.mobile.changePage(a(h),i.options)}else{a.mobile.changePage(a(h),i.options)}j.preventDefault()}}});if(d.fixFirstPageDataUrl){a(document).ready(function(){var h=a(":jqmData(role='page')").first();var i=h.jqmData("url"),g=window.location.pathname+d.firstPageDataUrl+window.location.search+window.location.hash;if(!window.location.pathname.match("/$")){return}if(i!=g){h.attr("data-url",g).jqmData("url",g)}})}a.mobile.Router=function(k,h,j){this.routes={pagebeforecreate:null,pagecreate:null,pagebeforeshow:null,pageshow:null,pagebeforehide:null,pagehide:null,pageinit:null,pageremove:null};this.evtLookup={bc:"pagebeforecreate",c:"pagecreate",bs:"pagebeforeshow",s:"pageshow",bh:"pagebeforehide",h:"pagehide",i:"pageinit",rm:"pageremove"};this.routesRex={};this.conf=a.extend({},d,j||{});this.defaultHandlerEvents={};if(this.conf.defaultHandlerEvents){var g=this.conf.defaultHandlerEvents.split(",");for(var l=0;l<g.length;l++){this.defaultHandlerEvents[this.evtLookup[g[l]]]=g[l]}}this.add(k,h)};a.extend(a.mobile.Router.prototype,{add:function(j,i){if(!j){return}var g=this,h=[];if(j instanceof Array){a.each(j,a.proxy(function(m,l){this.add(l,i)},this))}else{a.each(j,function(o,m){if(typeof(m)=="string"||typeof(m)=="function"){if(g.routes.pagebeforeshow===null){g.routes.pagebeforeshow={}}g.routes.pagebeforeshow[o]=m;if(!g.routesRex.hasOwnProperty(o)){g.routesRex[o]=new RegExp(o)}}else{var l,n=m.events.split(","),k;for(l=0;l<n.length;l++){k=g.evtLookup[n[l]];if(g.routes.hasOwnProperty(k)){if(g.routes[k]===null){g.routes[k]={}}g.routes[k][o]=m.handler;if(!g.routesRex.hasOwnProperty(o)){g.routesRex[o]=new RegExp(o)}}else{b("can't set unsupported route "+n[l])}}}});a.each(g.routes,function(k,l){if(l!==null){h.push(k)}});if(!this.userHandlers){this.userHandlers={}}a.extend(this.userHandlers,i||{});this._detachEvents();if(h.length>0){this._liveData={events:h.join(" "),handler:function(l,k){g._processRoutes(l,k,this)}};a(":jqmData(role='page'),:jqmData(role='dialog')").live(this._liveData.events,this._liveData.handler)}}},_processRoutes:function(l,p,m){var n=this,o,h,k,g=0;if(l.type in {pagebeforehide:true,pagehide:true,pageremove:true}){o=e}else{o=f}do{if(!o){if(m){k=a(m);o=k.jqmData("url");if(o){if(k.attr("id")==o){o="#"+o}o=a.mobile.path.parseUrl(o)}}}else{if(m&&!a(m).jqmData("url")){return}}if(!o){return}h=(!this.conf.ajaxApp?o.hash:o.pathname+o.search+o.hash);if(h.length==0){o=""}g++}while(h.length==0&&g<=1);var j=false;a.each(this.routes[l.type],function(q,s){var r,u;if((r=h.match(n.routesRex[q]))){if(typeof(s)=="function"){u=s}else{if(typeof(n.userHandlers[s])=="function"){u=n.userHandlers[s]}}if(u){try{u(l.type,r,p,m,l);j=true}catch(t){b(t)}}}});if(!j&&this.conf.defaultHandler&&this.defaultHandlerEvents[l.type]){if(typeof(this.conf.defaultHandler)=="function"){try{this.conf.defaultHandler(l.type,p,m,l)}catch(i){b(i)}}}},_detachEvents:function(){if(this._liveData){a(":jqmData(role='page'),:jqmData(role='dialog')").die(this._liveData.events,this._liveData.handler)}},destroy:function(){this._detachEvents();this.routes=this.routesRex=null},getParams:function(g){if(!g){return null}var j={},h;var i=g.slice(g.indexOf("?")+1).split("&");a.each(i,function(m,l){h=l.split("=");if(j[h[0]]){if(!(j[h[0]] instanceof Array)){j[h[0]]=[j[h[0]]]}j[h[0]].push(h[1])}else{j[h[0]]=h[1]}});if(a.isEmptyObject(j)){return null}return j}})})})(jQuery);
+(function(a){a(document).bind("mobileinit",function(){var d=a.extend({fixFirstPageDataUrl:false,firstPageDataUrl:"index.html",ajaxApp:false},a.mobile.jqmRouter||{});var c=true;function b(g){if(c){console.log(g)}}var e=null,f=null;a(document).bind("pagebeforechange",function(j,i){if(typeof i.toPage==="string"){var g=a.mobile.path.parseUrl(i.toPage);e=f;f=g;if(g.hash.indexOf("?")!==-1){var h=g.hash.replace(/\?.*$/,"");i.options.dataUrl=g.href;if(a.mobile.activePage&&h.replace(/^#/,"")==a.mobile.activePage.jqmData("url")){i.options.allowSamePageTransition=true;a.mobile.changePage(a(h),i.options)}else{a.mobile.changePage(a(h),i.options)}j.preventDefault()}}});if(d.fixFirstPageDataUrl){a(document).ready(function(){if(!window.location.pathname.match("/$")){return}var h=a(":jqmData(role='page')").first();var i=h.jqmData("url"),g=window.location.pathname+d.firstPageDataUrl+window.location.search+window.location.hash;if(i!=g){h.attr("data-url",g).jqmData("url",g)}})}a.mobile.Router=function(k,h,j){this.routes={pagebeforecreate:null,pagecreate:null,pagebeforeshow:null,pageshow:null,pagebeforehide:null,pagehide:null,pageinit:null,pageremove:null};this.evtLookup={bc:"pagebeforecreate",c:"pagecreate",bs:"pagebeforeshow",s:"pageshow",bh:"pagebeforehide",h:"pagehide",i:"pageinit",rm:"pageremove"};this.routesRex={};this.conf=a.extend({},d,j||{});this.defaultHandlerEvents={};if(this.conf.defaultHandlerEvents){var g=this.conf.defaultHandlerEvents.split(",");for(var l=0;l<g.length;l++){this.defaultHandlerEvents[this.evtLookup[g[l]]]=g[l]}}this.add(k,h)};a.extend(a.mobile.Router.prototype,{add:function(j,i){if(!j){return}var g=this,h=[];if(j instanceof Array){a.each(j,a.proxy(function(m,l){this.add(l,i)},this))}else{a.each(j,function(o,m){if(typeof(m)=="string"||typeof(m)=="function"){if(g.routes.pagebeforeshow===null){g.routes.pagebeforeshow={}}g.routes.pagebeforeshow[o]=m;if(!g.routesRex.hasOwnProperty(o)){g.routesRex[o]=new RegExp(o)}}else{var l,n=m.events.split(","),k;for(l=0;l<n.length;l++){k=g.evtLookup[n[l]];if(g.routes.hasOwnProperty(k)){if(g.routes[k]===null){g.routes[k]={}}g.routes[k][o]=m.handler;if(!g.routesRex.hasOwnProperty(o)){g.routesRex[o]=new RegExp(o)}}else{b("can't set unsupported route "+n[l])}}}});a.each(g.routes,function(k,l){if(l!==null){h.push(k)}});if(!this.userHandlers){this.userHandlers={}}a.extend(this.userHandlers,i||{});this._detachEvents();if(h.length>0){this._liveData={events:h.join(" "),handler:function(l,k){g._processRoutes(l,k,this)}};a(":jqmData(role='page'),:jqmData(role='dialog')").live(this._liveData.events,this._liveData.handler)}}},_processRoutes:function(l,p,m){var n=this,o,h,k,g=0;if(l.type in {pagebeforehide:true,pagehide:true,pageremove:true}){o=e}else{o=f}do{if(!o){if(m){k=a(m);o=k.jqmData("url");if(o){if(k.attr("id")==o){o="#"+o}o=a.mobile.path.parseUrl(o)}}}else{if(m&&!a(m).jqmData("url")){return}}if(!o){return}h=(!this.conf.ajaxApp?o.hash:o.pathname+o.search+o.hash);if(h.length==0){o=""}g++}while(h.length==0&&g<=1);var j=false;a.each(this.routes[l.type],function(q,s){var r,u;if((r=h.match(n.routesRex[q]))){if(typeof(s)=="function"){u=s}else{if(typeof(n.userHandlers[s])=="function"){u=n.userHandlers[s]}}if(u){try{u(l.type,r,p,m,l);j=true}catch(t){b(t)}}}});if(!j&&this.conf.defaultHandler&&this.defaultHandlerEvents[l.type]){if(typeof(this.conf.defaultHandler)=="function"){try{this.conf.defaultHandler(l.type,p,m,l)}catch(i){b(i)}}}},_detachEvents:function(){if(this._liveData){a(":jqmData(role='page'),:jqmData(role='dialog')").die(this._liveData.events,this._liveData.handler)}},destroy:function(){this._detachEvents();this.routes=this.routesRex=null},getParams:function(g){if(!g){return null}var j={},h;var i=g.slice(g.indexOf("?")+1).split("&");a.each(i,function(m,l){h=l.split("=");if(j[h[0]]){if(!(j[h[0]] instanceof Array)){j[h[0]]=[j[h[0]]]}j[h[0]].push(h[1])}else{j[h[0]]=h[1]}});if(a.isEmptyObject(j)){return null}return j}})})})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.