Reputation: 6680
I'm creating my first Cordova plugin to work with an open source SDK. I'm having issues getting it running without callbacks, because the library doesn't have any (think something like analytics). There's also a bunch of different formats, and the wiki is 2 years old so I'm not sure as to what's the proper way to do things.
I've tried a few different variants of setting up the plugin, but none work successfully.
My .js:
;(function(){
if (Cordova.hasResource("myPlugin")) return
Cordova.addResource("myPlugin")
function MyPlugin() {
}
MyPlugin.prototype.setup = function(types) {
return Cordova.exec("MyPlugin.setup", types);
};
Cordova.addConstructor(function() {
if(!window.plugins)
{
window.plugins = {};
}
if (!window.plugins.myPlugin) {
window.plugins.myPlugin = new MyPlugin()
}
})
})();
The setup function takes 4 arguments, all strings. I include MyPlugin.js in the index.html file, then in onDeviceReady() I call:
window.plugins.myPlugin.setup('xxx-x-xxx','xxxx','xxxx','xxxx');
Whenever I try to run I get the error [INFO] Error in success callback: NetworkStatus0 = TypeError: 'undefined' is not an object
.
I have tried googling and whatnot, and found several different PhoneGap plugin formats, and I've tried them all, and none work. I'm using Cordova 1.6.1 if that helps.
Here's a list of different structures I've tried mimicking:
All of them produce the same error, so I'm not sure what the problem is.
EDIT: I should also mention, my Obj-C code does not get called. There's something up with the JS calls unrelated to the actual functionality of the plugin...
Upvotes: 2
Views: 5530
Reputation: 6680
Okay so for anyone else wondering about formatting...here's the solution I got that worked:
This works with 1.6.1 => 1.9x (but dont use 1.9 as it has a serious stack overflow
bug!)
var MyPlugin() = function() {};
MyPlugin.prototype.setup = function(types) {
return Cordova.exec("MyPlugin.setup", types);
};
cordova.addConstructor(function() {
if (!window.Cordova) {
window.Cordova = cordova;
};
if(!window.plugins) window.plugins = {};
window.plugins.myPlugin = new MyPlugin();
});
In Cordova 2.0 the addConstructor
has been removed and checking for Cordova is not necessary..
var MyPlugin() = function() {};
MyPlugin.prototype.setup = function(types) {
return Cordova.exec("MyPlugin.setup", types);
};
//Keep at bottom but remove the addConstructor for Cordova 2+
if(!window.plugins) window.plugins = {};
window.plugins.myPlugin = new MyPlugin();
Upvotes: 4