【问题标题】:Is there a way to detect the first page loading in Jquery Mobile from another plugin?有没有办法从另一个插件检测 Jquery Mobile 中的第一页加载?
【发布时间】:2012-06-17 16:48:55
【问题描述】:

我正在寻找一种在加载第一页之前利用 JQM 并覆盖内容的方法。

JQM 像这样初始化(来自最新的 JQM 代码):

( function( $, window, undefined ) {
    var $html = $( "html" ),
    $head = $( "head" ),
    $window = $( window );

    // trigger mobileinit event 
    $( window.document ).trigger( "mobileinit" );

    ... 

    $.extend($.mobile, {
        // find and enhance the pages in the dom and transition to the first page.
        initializePage: function() {

            ...
            // alert listeners that the pagecontainer has been determined for binding
            // to events triggered on it
            $window.trigger( "pagecontainercreate" );
            ...

因此,我可以利用 两个事件mobileinitpagecontainercreate。我知道这在 HTML 中很容易,如下所示:

<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
    $( window ).bind( 'pagecontainercreate', function() {
        console.log("HELLO");
        });
</script>
<script type="text/javascript" src="jquery.mobile-1.1.0.multiview_con.js"></script>   
<script type="text/javascript" src="plugin.js"></script>

但是如果我想在 JQM 之后从 INSIDE plugin.js 绑定到这些事件怎么办?如果我在 plugin.js 中放置相同的侦听器,它永远不会触发。我想,因为为时已晚。但是我需要防止默认加载第一页,所以一旦 JQM 尝试加载初始页面,对我来说就太晚了。

问题
在从 JQM 之后的插件开始加载第一页之前,有什么方法可以监听其中一个事件或任何其他事件触发?

编辑
我不能使用 pageinit,因为它会在调用第一页后触发。 pageCreate 的同上,它也不会从插件内部触发。

【问题讨论】:

  • 您是否尝试过将$(document).bind('mobileinit', function(){});$.mobile.autoInitializePage = false; 一起使用?
  • 我可以从插件内部运行它吗?我想我必须在标记中设置这个选项,比如here?
  • 您需要在 jQuery Mobile 包含之前包含 plugin.js 文件。或者在绑定到事件的 jQuery Mobile 文件之前至少包含一些代码。
  • @Jasper:不能,因为它经常使用 $.mobile."functions"。
  • 也许您可以将整个插件包装到 mobileinit 中?

标签: jquery events jquery-mobile event-listener


【解决方案1】:

好的。我用 JQMs autoinitialize 选项(see here)解决了这个问题,就像这样:

$(document).bind("mobileinit", function(){
$.mobile.autoInitializePage = false;
});

这种方式 Jquery Mobile 不会自动启动,但只有当我像这样手动调用它时:

 if ( $.mobile.autoInitializePage == false){
    $.mobile.initializePage();
    }

我已将最后一个 sn-p 放在我的第二个插件中,它需要在 JQM 触发第一页初始化之前完全设置。设置第二个插件后,我调用initializePage(),一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多