本教程中描述了解决此问题的方法:
http://andidog.de/blog/2012/06/using-jquery-mobile-with-backbone-how-to-solve-routing-conflicts-and-use-mvc-for-the-application/
在 router.js 中,更改这一行:
$.mobile.changePage($(view.el), {changeHash:false});
到这里:
$.mobile.changePage($(view.el), {transition: 'slide', changeHash:false});
或者在 jqm-config.js 中你可以像这样添加一个默认的页面转换:
$.mobile.defaultPageTransition = "slide";
现在,这将始终使用向前滑动(从右到左)过渡。如果您希望它在返回时以相反的方式滑动,您可以执行以下操作。这对我有用。这个 SO 答案帮助我弄清楚了:https://stackoverflow.com/a/11660991/1665818。在 router.js 中添加这些变量:
previousFragments: [],
backDetected: false,
并更改changePage函数:
changePage:function (view) {
//add the attribute 'data-role="page" ' for each view's div
view.$el.attr('data-role', 'page');
var currentFragment = Backbone.history.getFragment();
this.backDetected = false;
if(!window.linkClicked && currentFragment == this.previousFragments[this.previousFragments.length-2]) {
this.backDetected = true;
this.previousFragments.pop();
} else {
this.previousFragments.push(currentFragment);
}
//reset
window.linkClicked = false;
console.log("this.backDetected= "+this.backDetected);
//append to dom
$('body').append(view.$el);
if(!this.init){
$.mobile.changePage($(view.el), {reverse: this.backDetected, changeHash:false});
}else{
this.init = false;
}
}
然后在 main.js 顶部添加一个 window.linkClicked 变量:
window.linkClicked = false;
并在文档就绪函数中添加一个事件监听器:
$(document).ready(function() {
console.log("DOM IS READY");// Handler for .ready() called.
$(document).on('click', 'a', function() {
window.linkClicked = true;
});
});