jeanfrancois
jeanfrancois

Reputation: 76

Backbone.js + Jquery Mobile hash navigation not working

I am currently working on a mobile application based on the modular approach from :

http://weblog.bocoup.com/organizing-your-backbone-js-application-with-modules/

I am having some issues on the routing and i can't get my hand on what can be causing the problem.

My router definition is as follows :

    var AppRouter = Backbone.Router.extend({
    routes: {            
        'module1' : 'module1_home', // working        
        'module1view/:id' : 'module1_view',    //Not working                  
        // Default
        '*actions': 'defaultAction' //Working
    },
    initialize:function () {
        // Handle back button throughout the application
        $('.back').live('click', function(event) {
            window.history.back();
            return false;
        });
        this.firstPage = true;
    },
    module1_view : function(id)
    {        
        var module1 = MyApp.module('module1');
        var view = new module1.DisplayView;        
        this.changePage(view);            
    },    
    defaultAction : function () {
        var home = MyApp.module('home');
        var view = new home.DefaultView;  
        this.changePage(view);
    },
    changePage:function (page) {        
        $(page.el).attr('data-role', 'page');
        page.render();
        $('body').append($(page.el));
        var transition = 'slide';

        if (this.firstPage) {
            transition = 'none';
            this.firstPage = false;
        }
        $.mobile.changePage($(page.el), {
            changeHash:true, 
            transition: transition
        });
    }
});

Here are example URL and the results

http://localhost:12345/myapp/ => shows the default view
http://localhost:12345/myapp/index.html => shows the default view
http://localhost:12345/myapp/#module1 => shows module1 home view
http://localhost:12345/myapp/index.html#module1 => shows module1 home view

http://localhost:12345/myapp/#module1view/123 => url changes to http://localhost:12345/myapp/module1view/123 and displays the default view
http://localhost:12345/myapp/index.html#module1/123 => url changes to http://localhost:12345/myapp/module1view/123 and displays the default view

I have also deactivated JQM navigation using

jQuery(function($) {    
    $(document).on("mobileinit", function () {
        $.mobile.ajaxEnabled = false;
        $.mobile.linkBindingEnabled = false;
        $.mobile.hashListeningEnabled = false;
        $.mobile.pushStateEnabled = false;

        // Remove page from DOM when it's being replaced
        $('div[data-role="page"]').live('pagehide', function (event, ui) {
            $(event.currentTarget).remove();
        });
    });          
});

I also using WAMP as web server here and firefox as test browser.

Did someone have similar issues and can give me some insights on how to solve this problem ?

Update#1: Some complementary information, the alert is not called here :

module1_view : function(id)
    { 

        alert(id);
    }

Thus I may think that something is going on upper in the code.

Upvotes: 1

Views: 1154

Answers (1)

jeanfrancois
jeanfrancois

Reputation: 76

I was able to find the problem. It seems that putting the JQM settings inside :

jQuery(function($) {    });
or
$(function() { });

did not trigger the doc $(document).on("mobileinit", function () { }); for some reason. I still need to investigate on this one.

But removing triggers the command and JQM navigation is disable and i can use backbone router properly.

Upvotes: 1

Related Questions