Backbone.js + Jquery移动哈希导航无法正常工作

时间:2012-08-31 09:49:38

标签: jquery jquery-mobile backbone.js

我目前正致力于基于模块化方法的移动应用程序:

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

我在路由方面遇到了一些问题,但我无法理解可能导致问题的原因。

我的路由器定义如下:

    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
        });
    }
});

以下是示例网址和结果

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

我还使用

停用了JQM导航
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();
        });
    });          
});

我也在这里使用WAMP作为Web服务器,使用firefox作为测试浏览器。

是否有人有类似的问题,可以就如何解决这个问题给我一些见解?

更新#1: 一些补充信息,此处不会调用警报:

module1_view : function(id)
    { 

        alert(id);
    }

因此,我可能会认为代码中的某些内容正在发生。

1 个答案:

答案 0 :(得分:1)

我能够找到问题所在。似乎将JQM设置放在:

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

没有触发doc $(document).on(“mobileinit”,function(){});由于某些原因。我还需要对此进行调查。

但是删除触发命令并禁用JQM导航,我可以正确使用骨干路由器。