当省略data-main属性时,Require.js无法在Phonegap应用程序中运行

时间:2014-01-23 18:08:12

标签: cordova requirejs

我正在开发一个带有Require.js的Phonegap应用程序。在我的index.html中,我有:

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

<script type="text/javascript" src="js/app/index.js"></script>
<script type="text/javascript">
        app.initialize();  
</script>

index.js文件不能成为Require模块的一部分,这是我详细解决的问题here。我希望在Require.js设置所有配置内容后执行app.initialize()。我使用了解决方案here。所以它变成了:

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

<script src="js/require.js"></script>
<script src="js/require-config.js"></script>
<script>
    require(["app"]);
</script>

<script type="text/javascript" src="js/app/index.js"></script>
<script type="text/javascript"> 
        app.initialize();
</script>

因此省略了“data-main”,这意味着Require stuff会立即运行。这在浏览器中完美运行。但由于某些原因,当作为Phonegap应用运行时,它会生成需要脚本错误:

Uncaught Error: Script error for: app

在Phonegap应用程序中省略“data-main”时,似乎无法运行。这是真的吗?

修改

index.js文件是:

var app = {
// Application Constructor
initialize: function() {

    this.bindEvents();

},

// Bind Event Listeners

bindEvents: function() {

    document.addEventListener('deviceready', this.onDeviceReady, false);
},


/*
 * This function registers the device with the server, and stores the device id and the api key.
 * This should only ever execute once. 
 */
registerDeviceWithServer: function(reg_id){

        var url = "http://push.theserver.ie/device_api/device";
        //var url = "http://localhost/theserver/device_api/device";

        $.ajax({
            url: url,
            type: "post",
            data: {project_title: 'mountmercy', platform: window.device.platform},
            pure_ajax: true,
            headers :{device_id:"63843",
            api_key:"hv7Vgd4jsbb"},
            success: function(data){
                //alert("success in registerDeviceWithServer and id is");

                var obj = jQuery.parseJSON(data);

                var device_id = obj.id;
                var api_key = obj.api_key;
                window.localStorage.setItem('mountmercy_device_id', device_id);
                window.localStorage.setItem('mountmercy_api_key', api_key);

                //now update the Reg Id
                app.updateRegId(device_id, api_key, reg_id);
            },
            error:   function(model, xhr, options){
                //alert('failed in registerDeviceWithServer');
                // alert('in Error');
                console.log('response is : ');
                console.log(app.logObject(xhr));
            },
        });
},


updateRegId: function(device_id, api_key, reg_id){

        var url = "http://push.theserver.ie/device_api/device/"+device_id;

        console.log('in updateRegId');
        console.log('updatig with reg_id of ');
        console.log(reg_id);
        $.ajax({
            url: url,
            type: "put",
            data: {reg_id: reg_id},
            pure_ajax: true,
            headers :{device_id:device_id,
            api_key:api_key},
            success: function(data){
                //alert("success in updateRegId");

            },
            error:   function(model, xhr, options){
                //alert('failed in updateRegId');
                // alert('in Error');
                console.log('response is : ');
                console.log(app.logObject(xhr));
            },
        });

        console.log('after the update ajax');
},


// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {

    console.log('in onDeviceReady and platform is ');
    console.log(window.device.platform);
    var pushNotification = window.plugins.pushNotification;
    if (window.device.platform == 'android' || window.device.platform == 'Android') {
        pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"475226855592","ecb":"app.onNotificationGCM"});                        
    }
    else{
        //so its apple
         pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
    }

},


// result contains any message sent from the plugin call
successHandler: function(result) {
   //alert('Callback Success! Result = '+result)
},
errorHandler:function(error) { 
    //alert('in errorHandler');
    //alert(error);
},

/*
 * 
 * For iOS
 */        
tokenHandler:function(status) {


    var device_id = window.localStorage.getItem('mountmercy_device_id');
    var api_key = window.localStorage.getItem('mountmercy_api_key');

    if(typeof(device_id)==='undefined' || device_id===null){
        //we dont have a device id so register it and save to local storage. 
        //should only ever enter here once     
        console.log('in the if so going to registerDeviceWithServer ');

        app.registerDeviceWithServer(status);        

    }
    else{
        app.updateRegId(device_id, api_key, status);

    }

},


/*
 * For Android Phones
 */
onNotificationGCM: function(e) {

    switch( e.event )
    {

        case 'registered':
            if ( e.regid.length > 0 )
            {

                var device_id = window.localStorage.getItem('mountmercy_device_id');
                var api_key = window.localStorage.getItem('mountmercy_api_key');


                if(typeof(device_id)==='undefined' || device_id===null){
                    //we dont have a device id so register it and save to local storage. 
                    //should only ever enter here once     
                    console.log('in the if so going to registerDeviceWithServer ');

                    app.registerDeviceWithServer(e.regid);        

                }
                else{
                    //so we have already registered device on server. Now update reg_id
                    //console.log('in the else so going to updateRegId and app.reg_id is ');
                    //console.log(app.reg_id);
                    app.updateRegId(device_id, api_key, e.regid);

                }

            }
            break;

        case 'message':

            window.location.hash = "article/"+e.payload.article_id;

            break;

        case 'error':
            //alert('GCM error = '+e.msg);
            break;

        default:
           // alert('An unknown GCM event has occurred');
            break;
    }
}, 


onNotificationAPN: function(event) {

    console.log('*******event is ************');
    console.log(app.logObject(event));

    var pushNotification = window.plugins.pushNotification;
    if (event.alert) {
        navigator.notification.alert(event.alert);
    }
    if (event.badge) {
        console.log("Set badge on  " + pushNotification);
        pushNotification.setApplicationIconBadgeNumber(this.successHandler, event.badge);
    }
    if (event.sound) {
        var snd = new Media(event.sound);
        snd.play();
    }
},

};

1 个答案:

答案 0 :(得分:0)

似乎你忘了在js / require.js之前加入cordova.js。

<强>更新 加载后尝试使用应用程序。 (假设app在app.js模块的某处返回)

<script type="text/javascript" src="js/app/index.js"></script>
<script type="text/javascript"> 
    require(["app"], function(app) {
         app.initialize();
    });

</script>