Search code examples
javascriptcordovaphonegap-plugins

alternative to cordova.addConstructor?


I am using a PhoneGap Plug in with register itself like this

// Register the plugin

cordova.addConstructor(function () {
  window.pushNotification =  new PushNotification();
});

I need to use CORDOVA at VERSION 2.5.0, so for my understanding cordova.addConstructor( has been removed in version.

Which API is more appropriate instead taking in consideration what must be also compatible with CORDOVA VERSION 2.1.0?


Solution

  • You don't need to use addConstructor anymore, just do:

            if (!window.plugins) {
                window.plugins = {};
            }
            window.plugins.pushNotification = new PushNotification();
    

    Or I guess since you don't seem to be using a plugins object, just window.pushNotification = new PushNotification()

    I tested this with a mock application on 2.0 and 2.5 and both worked.

    Although you asked for 2.5, I just want to point out that when you go to update this plugin for use with 2.6, it is recommended to wrap your plugin in a module.