Hi all,
I'm relatively new to JQM (been working with it for the past month or two), but I thought I'd offer my two cents anyways.
I am using JQM to build a dynamic app. Mine requires loading some JSON containing templates via jsonp and building JQM pages from them, with custom behavior defined in the JSON attached to the created pages (event handlers and such).
For the purpose of a POC I decided to skip the templating part (since it's a solved problem that has nothing to do with JQM), but to still load and build the pages a dynamically. After some playing around I resorted to disabling all handling of clicks/history by JQM, and writing
my own getPage method which does the JSONP call and then runs a callback function to attache the behavior to the requested page.I keep track of my app's state using a cookie.
My getPage function issues a request to a simple service that serves my finished JQM html according to a "UID". The returned html may contain more than a single JQM page, so I use a "pageId" to identify what internal page to navigate to. After loading the page an optional callback is attached to the "pageshow" event of the page. This contains the specific logic for handling each page's behavior (event handlers and such). Of course, getPage first checks to see if the page already exists in the dom before trying to fetch it, and skips the JSONP request if it does.
The code for getPage looks like this
function getPage(UID,pageId,callback){
var page;
if($('#'+pageId).length>0){
//callback is executed after page is loaded
if (callback !== undefined){
$('#'+pageId).one('pageshow',function(event, ui){
callback();
});
}
$.mobile.changePage(pageId, "none", false, false);
}else{
$.getJSON("http://foo.bar/getPage?UID="+UID+"&callback=?",function(pageObj) {
if (pageObj.success)
{
page=$(pageObj.data.html);
$('body').append(page);
$.mobile.initializePage();
//callback is executed after page is loaded and before state is changed
if (callback !== undefined){
$('#'+pageId).one('pageshow',function(event, ui){
callback();
});
}
$.mobile.changePage(pageId, "none", false, false);
}
});
}
}
(ps. I know that I could bind callback directly, I do it this way as a preparation to the time I enhance the logic to use the prev/next page info from the ui object)
The actual calls to getPage are of course done by my own event handlers, so I never really use anchors for navigation between pages.
I think that it could be great for my purposes if the hijaxing behavior of JQM would have been more customizable, with the possibility to define some custom changePage wrapper such as my getPage function. something along the lines of $.mobile.changePageWrapper=function(event,ui){}; taht would be called instead of changePage when defined.
An alternative is to add a "beforechangepage" event handler where one could handle the request if needed and return false to cancel the changePage and only issue changePage manually after the request succeeds.
For both of these options it might be a good idea to be able to tell JQM to use this behavior for a specific anchor by defining some property for it. Either as an opt in or opt out, or both.
Adding the custom logic for each page would still be done using the normal page events, and the binding could be done in the beforechangepage or the changePage wrapper.
I hope this makes some sense, as I disclaimed above, I'm pretty noob in JQM :)