Cordova 2.2.0 on iOS - RequireJS won't load Cordova correctly

后端 未结 3 2013
忘掉有多难
忘掉有多难 2020-12-15 10:57

I\'m currently developing a webapp using Cordova (Phonegap), Backbone and JQtouch. Among other things, I need to add events in the user calendar.

Everything works fi

3条回答
  •  误落风尘
    2020-12-15 11:57

    In your require.config object, you need to export cordova via the shim attribute:

    require.config({
       baseUrl: 'js',
       paths: {
          cordova: '../lib/cordova/cordova-2.2.0'
       },
       shim: {
          cordova: {
             exports: 'cordova'
          }
       }
    });
    

    It is best to define a module to access cordova's exec module:

    /*global define */
    
    define(['cordova'], function (cordova) {
       'use strict';
    
       return cordova.require('cordova/exec');
    });
    

    Creating a custom plugin is now easy:

    /*global define */
    
    define(['plugins/cordovaExec'], function (cordovaExec) {
       'use strict';
    
       return function showToast(callback, message) {
          cordovaExec(callback, function (error) {}, "Toaster", "show", [message]);
       };
    });
    

    You could do it in one module only:

    /*global define */
    
    define(['cordova'], function (cordova) {
       'use strict';
    
       var exec = cordova.require('cordova/exec');
    
       return function showToast(callback, message) {
          exec(callback, function (error) {}, "Toaster", "show", [message]);
       };
    });
    

    Hope that helps :)

提交回复
热议问题