Cordova 使用require.js通过deviceready事件初始化phonegap

Cordova 使用require.js通过deviceready事件初始化phonegap,cordova,requirejs,Cordova,Requirejs,我想找到一种方法,通过我正在收听的deviceready事件初始化我的应用程序 我要加载以下javascript文件: //index.html <head> <script src="cordova.js"> <script src="require.js" data-main="main.js"> </head> 然后回到my index.html中: <body> <script>

我想找到一种方法,通过我正在收听的deviceready事件初始化我的应用程序

我要加载以下javascript文件:

//index.html
<head>
    <script src="cordova.js">
    <script src="require.js" data-main="main.js">
</head>
然后回到my index.html中:

<body>
    <script>
         function onDeviceReady() {
             myApp.init();
         }
    </script>
 </body>

函数ondevicerady(){
myApp.init();
}
我不知道这样行不行。
如何使用requirejs初始化phonegap应用程序?

此方法的问题在于它会污染环境 全局名称空间,而且过于复杂。为什么不直接要求呢 应用程序在您的设备就绪回调

<body>
    <script>
         function onDeviceReady() {
           require(['app'], function(App) {
             app.init()
           } 
         }
    </script>
</body>

函数ondevicerady(){
需要(['app'],函数(app){
app.init()
} 
}
然后你甚至不需要main.js!(除非你想添加一些
配置)。

此方法的问题在于它会污染环境 全局名称空间,而且过于复杂。为什么不要求 应用程序在您的设备就绪回调

<body>
    <script>
         function onDeviceReady() {
           require(['app'], function(App) {
             app.init()
           } 
         }
    </script>
</body>

函数ondevicerady(){
需要(['app'],函数(app){
app.init()
} 
}
然后你甚至不需要main.js!(除非你想添加一些
配置)。

事件侦听器可以添加到我的主模块中。 因此,应用程序由主模块的deviceready事件初始化,如下所示:

//main.js
var myApp = {};
define(['app'],
    function(app){
       var init = function(){
             app.run();
        }
        myApp.init = init;
    }
);
require([
'config/RootPathConfig',
'app',
'overrides'
], function(rootPath, app){

    document.addEventListener("deviceready",onDeviceReady,false);

    function onDeviceReady() {
        console.log("deviceReady");
        rootPath.initialize();
        app.init();                         //now the app content is loaded after the device is ready :)
    }

});

事件侦听器可以添加到我的主模块中。 因此,应用程序由主模块的deviceready事件初始化,如下所示:

//main.js
var myApp = {};
define(['app'],
    function(app){
       var init = function(){
             app.run();
        }
        myApp.init = init;
    }
);
require([
'config/RootPathConfig',
'app',
'overrides'
], function(rootPath, app){

    document.addEventListener("deviceready",onDeviceReady,false);

    function onDeviceReady() {
        console.log("deviceReady");
        rootPath.initialize();
        app.init();                         //now the app content is loaded after the device is ready :)
    }

});

另一个解决方案是使用承诺:

ondevicerady.js

define(函数(){
返回新承诺(函数(解析、拒绝){
如果(document.URL.match(/^https?:/i)){//credits tohttp://stackoverflow.com/a/12255930/1225328
log(“在浏览器中运行…”);
解决();
}否则{
log(“在应用程序中运行…”);
文件。添加的监听器(“DeviceRady”,resolve,false);
}
});
});
main.js

define([“ondevicerady”,“app”],函数(ondevicerady,app){
ondevicerady.then(函数(){
//设备已就绪,请引导您的应用程序:
app.run();
});
});

另一个解决方案是使用承诺:

ondevicerady.js

define(函数(){
返回新承诺(函数(解析、拒绝){
如果(document.URL.match(/^https?:/i)){//credits tohttp://stackoverflow.com/a/12255930/1225328
log(“在浏览器中运行…”);
解决();
}否则{
log(“在应用程序中运行…”);
文件。添加的监听器(“DeviceRady”,resolve,false);
}
});
});
main.js

define([“ondevicerady”,“app”],函数(ondevicerady,app){
ondevicerady.then(函数(){
//设备已就绪,请引导您的应用程序:
app.run();
});
});

我想问题是require.js需要一个模块作为xyz.js作为引用来应用。所以我需要main.js(模块)由于这里的关系:data main属性是可选的。欢迎您使用它并将配置信息放在文件main.js中,但只有在需要的情况下。所以我可以将其保留为空?我明天会尝试。到目前为止,thx很多。将data main保留为空不起作用。不幸的是,它不会加载任何内容。我想问题在于require.js需要一个模块xyz.js作为引用来应用由于这里的关系:data main属性是可选的。欢迎您使用它并将配置信息放在文件main.js中,但只有在需要的时候。所以我可以将其保留为空?我明天会尝试。到目前为止,thx很多。将data main保留为空不起作用。不幸的是,它不会加载任何内容。