azicchetti / jquerymobile-router

A router/controller for jquery mobile. Also adds support for client-side parameters in the hash part of the url. The routes handles regexp based routes. This plugin can be used alone or (better) with Backbone.js or Spine.js, because it's originally meant to replace their router with something integrated with jQM.
GNU General Public License v2.0
402 stars 69 forks source link

Not an issue actually but the request to provide more advanced example #66

Open aefimov64 opened 11 years ago

aefimov64 commented 11 years ago

Dear Andrea,

It would be great if you provide more advanced example that demonstrates the whole power of your plugin. I.e. an example that intercepts ALL jQM page events starting from the beginning.

For me, I'm struggling with proper configuration of the router to intercept pagebeforechange event when first jQM page located in my index.html is processed/displayed by jQM. Everething is fine when I start navigating to other pages. I can intercept the whole bunch if events.

azicchetti commented 11 years ago

Hi @aefimov64 I was planning to make the examples a little bit more useful and hopefully I'll find some time in the next weeks. In the meanwhile, I'm glad to help you fix your specific issue.

As per the pagebeforechange event, I'll probably remove the support from the router, because (but this is only my opinion) it shouldn't be used in normal applications, at least without very good reasons. Other events are specifically designed to provide certain "hook points" in the jQM page transition flow and are probably the best choice in normal applications.

aefimov64 commented 11 years ago

Thanks a lot for your feedback! I do really want this event (pagebeforechange) to be supported by your excellent plugin. Just one example to illustrate why do I need it. May be you will like this idea too :)

I want page structure in the app to be as much flexibility as possible. Even more I want this structure to be downloadable from my server. I also want my app to be launched with "blank" body because I cannot predict page structure that I will receive on app start from the server!

Armored with your plugin I'm going to setup a set of routers that will help me to intercept pagebeforechange events to populate blank body with requested jQM page on the fly and then navigate to it. This is why I need pagebeforechange event. So the sequence is:

azicchetti commented 11 years ago

I've done some applications that are just like what you've just described but I used a different approach.

Basically what you're trying to do is already done by jquery mobile itself when you use the single page template in "ajax mode". When a certain page is requested on the client, jquery mobile performs an ajax request to the server, grabs the html and displays the newly created page to the user. Also, when the user navigates away, the old page is automatically removed by the framework to keep the dom as small as possible.

The html that is fetched from the server can be a simple skeleton and may contain underscore templates, which is very useful to keep the javascript tidy and avoid long strings that contain html code. I find this really useful. You may populate the page dynamically on the client with regular backbone views. You can also keep the pages in the dom if you want, just add a route for the pageremove event and call e.preventDefault().

There are, however, some edge cases where you want to append the page yourself, without fetching anything from the server. You can do this by binding to the pagebeforeload event and doing some things with the event and the associated deferred object (basically, you just have to call e.preventDefault(), then resolve the deferred in the eventdata object). This is, more or less, the "jquery mobile way", and probably the simplest and fastest way to dynamically inject pages with this framework.

The only limit of this technique is that you have to follow these simple rules:

Hope this is somehow useful. Cheers, Andrea