Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Execute handler functions with scope ControllerObject #22

Merged
merged 1 commit into from

2 participants

@ivorbosloper

I would like handler functions to be executed in the ControllerObject scope (instead of the current no-scope). This allows you to use this in the ControllerObject/handler functions. Additionally, you could extend handlers with a scope keyword.

var router=new $.mobile.Router([
  { "#page": { handler: "page", events: "s" }},
  { "#test": { handler: "test", events: "s" }}],
  {
    sharedFunction: function(page) {
      // initialize footer on page
      $(page).append('<div>footer</div>');
    },
    page: function(eventType, matchObj, ui, page, evt) {
      this.sharedFunction(page);
      // other logic
    }
  }
]);
@ivorbosloper

I'm trying to learn git/github, so let me know if I did something wrong...

@azicchetti
Owner

Perfect, thanks for the patch.

I'll fix a small potential issue in a further commit, because the ControllerObject passed to the router is now just copied into an empty object {} [ cfr: $.extend(this.userHandlers,userHandlers||{}) ].
It's better to keep a reference to the original object, if provided by the user.

@azicchetti azicchetti merged commit 3661d34 into azicchetti:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 11, 2012
  1. @ivorbosloper

    Execute handler functions in the ControllerObject scope, allows you t…

    ivorbosloper authored
    …o use this in handler functions
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 5 deletions.
  1. +3 −3 js/jquery.mobile.router.js
  2. +2 −2 js/jquery.mobile.router.min.js
View
6 js/jquery.mobile.router.js
@@ -1,4 +1,4 @@
-/*
+/*!
* jQueryMobile-router v0.6
* http://github.com/azicchetti/jquerymobile-router
*
@@ -227,7 +227,7 @@ $(document).bind("mobileinit",function(){
handleFn=_self.userHandlers[handler];
}
if (handleFn){
- try { handleFn(e.type,res,ui,page,e); bHandled = true;
+ try { handleFn.apply(_self.userHandlers, [e.type,res,ui,page,e]); bHandled = true;
}catch(err){ debug(err); }
}
}
@@ -236,7 +236,7 @@ $(document).bind("mobileinit",function(){
if (!bHandled && this.conf.defaultHandler && this.defaultHandlerEvents[e.type]) {
if (typeof(this.conf.defaultHandler) == "function") {
try {
- this.conf.defaultHandler(e.type, ui, page, e);
+ this.conf.defaultHandler.apply(_self.userHandlers, [e.type, ui, page, e]);
} catch(err) { debug(err); }
}
}
View
4 js/jquery.mobile.router.min.js
@@ -1,4 +1,4 @@
-/*
+/*!
* jQueryMobile-router v0.6
* http://github.com/azicchetti/jquerymobile-router
*
@@ -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(){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);
+(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.apply(n.userHandlers,[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.apply(n.userHandlers,[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);
Something went wrong with that request. Please try again.