2012-07-23 151 views
0

后取消绑定事件我想,当我改变页面取消绑定的所有事件。我把this solution与this.unbind()调用扩展视图的关闭功能,我试图把它与JQM页面过渡在changePage功能的路由器从here结合:骨干和jQuery Mobile的:一个页面过渡

changePage: function(page){ 
     $(page.el).attr("data-role", "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}); 
    } 

然后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上pagehide甚至吨。我可以解除所有事件:$(event.currentTarget).unbind();?但是这也行不通。

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

回答

1

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

Backbone.View.prototype.close = function() { 
    if (this.beforeClose) { 
     this.beforeClose(); 
    } 
    this.remove(); 
    this.unbind(); 
}; 

如果您在接近方法评论this.remove(),它的工作原理。

另一种选择是评论$(event.currentTarget).remove();pagehide jqmobile事件和密切方法不做评论this.remove()

你不能两者都做,你应该选择的两个选项之一。我更喜欢第二种选择,但我认为它与第一种选择类似。我不建议在页面隐藏事件上致电unbind()

0

我面临同样的问题,由于某些原因,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(); 
    }); 
}); 

我花了几个小时,this SO跟帖得到这个。希望这可以帮助某人。