问题与动态加载phonegap.js [英] Issue with dynamically loaded phonegap.js

查看:227
本文介绍了问题与动态加载phonegap.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态加载phonegap javascript文件(所以我可以选择不在调试模式下加载它,当我使用Ripple),但我正在运行一些问题。



我使用正常的脚本标记加载jquery和jquerymobile javascript库。在另一个脚本块中,我执行:

  function onDeviceReady(){
alert(Device Ready!
}
$(document).ready(function(){
alert(doc ready!);
$ .getScript(js / phonegap.0.9.5.1。 js,function(){alert(Got Phonegap!);});
document.addEventListener(deviceready,onDeviceReady,false);
}

此代码警告Got Phonegap!但从不提示设备就绪。使用jsconsole.com,我可以看到PhoneGap javascript对象存在。但是,尝试调用device.uuid(或其他简单的phonegap API调用)失败。这几乎就像PhoneGap没有完全初始化。似乎不应该是这样的情况。我缺少什么?谢谢!

解决方案

我面临类似的问题,其中我需要根据平台类型加载PhoneGap和依赖的插件文件。我通过PhoneGap源,发现它使用Windows /浏览器事件来加载和准备对象。如果我手动调用浏览器事件,它将初始化我运行我的应用程序所需的PhoneGap对象(API和插件)。



使用Yabble的以下代码现在适用于我:

 < html> 
< head>
< script
src =https://raw.github.com/jbrantly/yabble/master/lib/yabble.js>< / script>

< script>
require.setModuleRoot(js);
require.useScriptTags();

require.ensure([jquery,phonegap],function(require){

//触发PhoneGap初始化
PhoneGap.onPhoneGapInit.fire );

//加载PhoneGap插件
require.ensure([plugin1],function(){
$(#console)。append(Plugin1 loaded< ; br>);
});

//以下函数仅在PhoneGap加载/初始化和插件成功注册时才起作用

检查PhoneGap设备对象
$(#checkDevice)click(function(){
console.log(JSON.stringify(device));
});

//调用本地插件
$(#callPlugin)。click(function(){
window.plugins.plugin1.call();
});
});
< / script>
< / head>
< body>
< div id =console>< / div>

< input type =buttonid =checkDevicevalue =检查设备>
< input type =buttonid =callPluginvalue =Call Plugin>
< / body>
< / html>

设备信息和插件调用在Android上正常工作。虽然我没有检查所有的PhoneGap API,但现在我只需要这两个工作,他们正在工作。



编辑 p>

在Phonegap 1.5 / Cordova, PhoneGap.onPhoneGapInit.fire(); 由于API更改不可用。在我当前的测试中,大多数必需的对象在没有任何更改,动态加载JS后可用。更新的测试可在此提供 - Cordova延迟负载测试


I'm trying to dynamically load the phonegap javascript file (so that I can choose not to load it in debug mode when I'm using Ripple) but I'm running in to some issues.

I load the jquery and jquerymobile javascript libraries using a normal script tag. In another script block, I do:

function onDeviceReady() { 
    alert("Device Ready!"); 
} 
$(document).ready(function() { 
    alert("doc ready!"); 
    $.getScript("js/phonegap.0.9.5.1.js", function() {alert("Got Phonegap!");}); 
    document.addEventListener("deviceready", onDeviceReady, false); 
}); 

This code alerts that it "Got Phonegap!" but never alerts "Device Ready". Using jsconsole.com, I can see that the PhoneGap javascript object exists. However, trying to call device.uuid (or other simple phonegap API calls) fails. It's almost like PhoneGap didn't fully initialize. Doesn't seem like that should be the case though. Am I missing something? Thanks!

解决方案

I was facing the similar issue where in which I need to load the PhoneGap and dependent plugin files based on the platform type. I went through the PhoneGap source and found that it uses the windows/browser events to load and prepare the objects. If I call the browser events manually then it initializes the PhoneGap objects (API and Plugins) I needed to run my application.

The following code which is using Yabble has worked for me now:

<html>
<head>
<script
    src="https://raw.github.com/jbrantly/yabble/master/lib/yabble.js"></script>

<script>
    require.setModuleRoot("js");
    require.useScriptTags();

    require.ensure([ "jquery", "phonegap" ], function(require) {

        // Trigger PhoneGap Initialization
        PhoneGap.onPhoneGapInit.fire();

        // Load PhoneGap Plugins
        require.ensure([ "plugin1" ], function() {
            $("#console").append("Plugin1 loaded<br>");
        });

        // Both following functions will work only if PhoneGap is loaded/initialized and Plugin is successfully registered

        // Check PhoneGap device object
        $("#checkDevice").click(function() {
            console.log(JSON.stringify(device));
        });

        // Call Native Plugin
        $("#callPlugin").click(function() {
            window.plugins.plugin1.call();
        });
    });
</script>
</head>
<body>
    <div id="console"></div>

    <input type="button" id="checkDevice" value="Check Device">
    <input type="button" id="callPlugin" value="Call Plugin">
</body>
</html>

Both Device info and Plugin calls is working fine on Android. Although I have not checked all the PhoneGap API but as of now I need only these two to work and they are working.

Edit

In Phonegap 1.5/Cordova, PhoneGap.onPhoneGapInit.fire(); is not available due to API change. In my current test most of the required objects are available without any change after loading the JS dynamically. Updated test is available at this gist - Cordova Lazy Load Test

这篇关于问题与动态加载phonegap.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆