应用程序在浏览器中工作正常,但不适用于cordova

时间:2016-02-29 12:13:46

标签: cordova requirejs

我在我的应用程序中使用Cordova 6和Requirejs。当我将我的应用程序启动到iPhone模拟器时,cordova deviceready事件被触发但我的代码通过require js加载永远不会被调用:(

我在自己的代码之前加载cordova,等待启动deviceready事件:

document.addEventListener('deviceready', function() {
    alert('this gets called with cordova so we know this works')
    define('app', ['router'], function(Router) {
        alert('in regular browser i get called, but not in cordova')
    });
}, false);

有没有办法解决这个问题?

2 个答案:

答案 0 :(得分:1)

对我来说有些东西是从require中删除data-main属性并直接加载我的主javascript文件,然后在deviceReady事件触发后使用require正常

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <script type="text/javascript" src="js/libs/require/require.js"></script>
</head>

<body>

  <script type="text/javascript" src="cordova.js"></script>
  <script type="text/javascript" src="app/init.js"></script>

</body>

</html>
var app = {

  initialize: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
  },

  onDeviceReady: function() {
    require(['app/requireModuleConfig'], function() {
      require([
          'core/shell',
          'domReady!'
        ],
        function(shell) {
          ko.applyBindings(shell);
          shell.Init();
        });
    });
  }
};

app.initialize();

requireModuleConfig.js

requirejs.config({

    baseUrl: window.location.pathname.replace('shell.html', '') + 'app/',
    //baseUrl: 'http://localhost:3001/app/',
    paths: {
        lib: '../js/libs',
        styles: '../content',
        text: '../js/libs/require/text',
        css: '../js/libs/require/css',
        domReady: '../js/libs/require/domReady',
        baseProxy: 'core/proxies/baseProxy',
        logger: '../js/libs/toastr/logger/logger',
        appContext: 'core/appContext',
        sync: 'core/sync/syncEngine',
        constants: 'core/constants/constants'
    },
    config: {
        text: {
            useXhr: function (url, protocol, hostname, port) {
                return true;
            }
        }
    }
});

答案 1 :(得分:0)

抱歉还不能发表评论。但我做了同样的事情 - 它确定了一个非常相似的问题。

<script type="text/javascript" src="libs/require.js"></script>
<script>
    document.addEventListener('deviceready', function() {
        require.config({
            baseUrl: 'js',
            paths: {
                handlebars: "../lib/handlebars/handlebars",
                text: "../lib/text/text",
                i18n: "../lib/i18n/i18n",
                hbs: "../lib/requirejs-hbs/hbs",
                moment: "../lib/moment/moment",
                moment_fr: "../lib/moment/locale/fr",
                moment_duration: "../lib/moment-duration-format/lib/moment-duration-format"
            },
            shim: {
                handlebars: {
                    exports: "Handlebars"
                }
            },
            config: {
                moment: {
                    noGlobal: true
                },
                i18n: {
                    locale: localStorage.getItem('locale') || 'en'
                }
            }
        });
        require(['init'], function(init) {
            init.coreApp();
        });
    }, false);

</script>