我使用的是带有JQueryMobile(1.0 beta 2)的backbone.js(0.5.3)。我知道在一起使用这些库时存在路由冲突,我想知道是否有解决方案可以使用它们:
我的问题与本文中描述的问题非常相似:jquery-mobile backbone.js routing
当我发出请求时,相应骨干视图的主干render
代码会在新的jquery页面完全加载之前被触发。我正在尝试在$(".ui-page-active")
DOM元素中呈现我生成的html代码,以定位由jQueryMobile(或“激活”的页面)生成的页面:
MyView = Backbone.View.extend({
el: $(".ui-page-active")
render: function(){
console.log(el)
}
});
但调用render方法时el
属性为空,因为jquery mobile尚未呈现dom ...
感谢您的帮助!
更新
Addy Osmani似乎有我的问题的答案:)但它将是他的(伟大的)教程的下一部分: http://msdn.microsoft.com/en-us/scriptjunkie/hh377172.aspx
答案 0 :(得分:10)
好的解决方案是禁用jQuery Mobile ajax加载功能并手动调用$.mobile.changePage
方法。
HTML页面
<script type="text/javascript" charset="utf-8" src="js/mobile/jquery.js"></script>
<script type="text/javascript">
$(document).bind("mobileinit", function(){
$.mobile.ajaxEnabled = false;
$.mobile.hashListeningEnabled = false;
});
</script>
<script type="text/javascript" charset="utf-8" src="js/mobile/jquery-mobile.js"></script>
然后每当触发新路由时,我首先在Backbone View构造函数中构建新的“jQuery页面画布”,将其附加到HTML文档body
并将我的el
视图元素设置为此新div
:
<强> Backbone.View 强>
$("body").prepend("""
<div id="my-id" data-role="page" class="cloudy-background-mobile">
<div class="cloudy-header" data-role="header" data-position="fixed"></div>
<div class="cloudy-content" data-role="content"></div>
</div>
""")
this.el = $("#logs-view")
在render
方法中:
// Build the content using undescore.js templating system
this.el.find('.cloudy-content').html(this.template({logs : this.collection}));
this.find('.cloudy-header').html(this.template_header({logbook: this.logbook}));
// Change the page using jquery mobile and reapply jquery styles
$.mobile.changePage(this.el, "slide", false, false);
this.trigger( "pagecreate" );
像魅力一样,没有任何不必要的黑客行为:)
以下是我的完整主干视图,如果它可以帮助任何人:
class LogsView extends Backbone.View
constructor: (options) ->
super
$("body").prepend("""
<div id="logs-view" data-role="page" class="cloudy-background-mobile">
<div class="cloudy-header" data-role="header" data-position="fixed"></div>
<div class="cloudy-content" data-role="content"></div>
</div>
""")
@el = $("#logs-view")
@logbook = options.logbook
@collection.bind 'reset', @render
@template = _.template('''
<ul data-role="listview" data-theme="c" data-inset="true">
<% logs.each(function(log){ %>
<li>
<a href="#logs-<%= log.cid %>"><%= log.getLabel() %></a>
</li>
<% }); %>
</ul>
''')
@template_header = _.template('''
<h1>Carnets <%= logbook.get('name') %></h1>
<a href="#logbook-<%= logbook.cid %>-logs-new" data-icon="plus" class="ui-btn-right"> </a>
''')
render: =>
# Build the content using undescore.js templating system
@el.find('.cloudy-content').html(@template({logs : @collection}))
@el.find('.cloudy-header').html(@template_header({logbook: @logbook}))
# Change the page using jquery mobile and reapply jquery styles
$.mobile.changePage(@el, "slide", false, false)
@el.trigger( "pagecreate" )
答案 1 :(得分:1)
这可能有点过分,因为我无法测试它,但尝试扩展Backbone的历史记录,让它在实际解雇代码之前监听创建事件。所以..
MyHistory = Backbone.History.extend({
loadUrl : function(fragmentOverride) {
var fragment = this.fragment = this.getFragment(fragmentOverride);
var matched = _.any(this.handlers, function(handler) {
if (handler.route.test(fragment)) {
//This is the only change from core code..
//We're just wrapping it into a callback.
$('.ui-page-active').one('pagecreate', function () {
handler.callback(fragment);
});
return true;
}
});
return matched;
}
});
MyHistory.start();
那可能会这样做,或者至少让你走上我希望的正确道路。
答案 2 :(得分:1)
使用jquery 1.2.0,禁用ajax和linkBinding
$(document).bind("mobileinit", function(){
$.mobile.ajaxEnabled = false;
$.mobile.hashListeningEnabled = false;
$.mobile.linkBindingEnabled = false;
$.mobile.pushStateEnabled = false;
});
之后,使用正常的Backbone路由,您可以将#id链接到
<a href="#id" onclick="window.app_router.navigate('new', true)">Report</a>