Jquery mobile 主干网和JQuery Mobile:页面转换后取消绑定事件

Jquery mobile 主干网和JQuery Mobile:页面转换后取消绑定事件,jquery-mobile,backbone.js,Jquery Mobile,Backbone.js,我想在更改页面时解除所有事件的绑定。我使用这个.unbind()调用扩展了视图的close函数,并尝试将它与路由器中的changePage函数中的JQM页面转换相结合,从: 然后changePage如下所示: changePage: function(page){ if (this.currentView) this.currentView.close(); $(page.el).attr("data-role", "page");

我想在更改页面时解除所有事件的绑定。我使用这个.unbind()调用扩展了视图的close函数,并尝试将它与路由器中的changePage函数中的JQM页面转换相结合,从:

然后changePage如下所示:

changePage: function(page){
        if (this.currentView)
            this.currentView.close();
        $(page.el).attr("data-role", "page");
        this.currentView = page;
        page.render();
        $("body").append($(page.el));
        var transition = $.mobile.defaultPageTransition;
        if(this.firstPage){
            transition = "none",
            this.firstPage = false;
        }
        $.mobile.changePage($(page.el), {changeHash: false, transition: transition});
    }
但是我得到了JQM错误:

Uncaught TypeError: Cannot call method '_trigger' of undefined jquery.mobile-1.1.0.js:2788
transitionPages jquery.mobile-1.1.0.js:2788
$.mobile.changePage jquery.mobile-1.1.0.js:3390
window.AppRouter.Backbone.Router.extend.changePage
我还有jqm-config.js,它删除了页面的DOM on pagehide事件。我是否可以在此处解除绑定所有事件,如:
$(event.currentTarget).unbind()?但这也不行

  $('div[data-role="page"]').live('pagehide', function (event, ui) {
    $(event.currentTarget).remove();
});

我也有同样的问题。发生JQM错误的原因是您试图在
close()
主干扩展方法中调用
this.remove()
,但事件“pagehide”已经从DOM中删除了视图

Backbone.View.prototype.close = function () {
    if (this.beforeClose) {
        this.beforeClose();
    }
    this.remove();
    this.unbind();
};
如果在close方法上注释this.remove()
,则该方法有效

另一个选项是注释
$(event.currentTarget).remove()隐藏jqmobile事件,并且在关闭方法时不注释此.remove()


你不能两者都做,你应该从两个选项中选择一个。我更喜欢第二种选择,但我认为它与第一种选择相似。我不建议对pagehide事件调用
unbind()

我也遇到了同样的问题,因为某些原因,pagechange事件没有被触发,以前的页面也没有从DOM中删除。一旦我删除了非活动页面,CSS又开始工作了

所以我补充说

    $('div[data-role="page"]').bind('pagehide', function (event, ui) {
        $(event.currentTarget).remove();
    });
里面

     $(document).bind('pagechange', function() {

     });
我的jqm-config.js如下所示

$(document).bind("mobileinit", function () {
     console.log('mobileinit');
     $.mobile.ajaxEnabled = false;
     $.mobile.linkBindingEnabled = false;
     $.mobile.hashListeningEnabled = false;
     $.mobile.pushStateEnabled = false;
//$.mobile.defaultPageTransition = "none";
});

  $(document).bind('pagechange', function() {
    $('div[data-role="page"]').bind('pagehide', function (event, ui) {
        console.log("Removing page");
        $(event.currentTarget).remove();
    });
});
我花了几个小时才弄到这个。希望这对别人有帮助

$(document).bind("mobileinit", function () {
     console.log('mobileinit');
     $.mobile.ajaxEnabled = false;
     $.mobile.linkBindingEnabled = false;
     $.mobile.hashListeningEnabled = false;
     $.mobile.pushStateEnabled = false;
//$.mobile.defaultPageTransition = "none";
});

  $(document).bind('pagechange', function() {
    $('div[data-role="page"]').bind('pagehide', function (event, ui) {
        console.log("Removing page");
        $(event.currentTarget).remove();
    });
});