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

default handler not called #70

Closed ghost closed 11 years ago

ghost commented 11 years ago

A typo has cost me a couple of hours to track done, since the default handler was not called:

return new $.mobile.Router({
            ... 

            "#tourselect"       : { handler: 'tourSelectHandler', events: 'bs'},

        }, {
                ...

                tourSelectHandler: function() {
                    require(['views/tour-select-view'], 
                        function(TourSelectView) {
                            new TourSelectView().render();
                        }
                    );
                },

                'default': function() {
                    console.log('No route found.');
                }

        }, {
                ajaxApp: true,
                defaultHandler: 'default'
           }
       );

Instead of tourSelectHandler the handler was tourselectHandler. Could that be a bug?

azicchetti commented 11 years ago

Hi, in order to use the defaultHandler option you should also define "defaultHandlerEvents". I think the documentation is lacking here, I'll fix it.

However, you found a bug, because the defaultHandler was only working with function references and not strings, like in your example. I'm pushing the fix on the git.

To sum this up, just add:

        defaultHandlerEvents: 'bs'

under the defaultHandler property and it should work.

p.s.: if you're using local pages, I guess you can remove the ajaxApp property

ghost commented 11 years ago

Thanks.

Another thing I run into is the following:

"#item"             : { handler: 'itemHandler', events: 'bs'},
"#itemgroup"     : { handler: 'itemGroupHandler', events: 'bs'}

If the itemgroup route is triggered the router first invokes itemHandler and then itemGroupHandler.

Am I doing something wrong?

Stephan

azicchetti commented 11 years ago

The "#item" string, when transformed into a regexp, matches both #item and #itemgroup.

To prevent this, you must put a $ to the end of your string, as in: #item$ This is pointed out in the documentation, just search the string: 'double check your regular expressions'.

To be on the safe side, always put "(?:[?](.*))?$" to the end of your routes:

{ "#item(?:[?](.*))?$": "itemHandler" },

This way you can also use the router.getParams() function to get url parameters in a nice javascript object.