Skip to content

Instantly share code, notes, and snippets.

@rikschennink
Created November 20, 2014 07:12
Show Gist options
  • Save rikschennink/bdc522f78f6a3cf3b3a8 to your computer and use it in GitHub Desktop.
Save rikschennink/bdc522f78f6a3cf3b3a8 to your computer and use it in GitHub Desktop.
Cordova Plugin API Defintion
(function(cordova){
'use strict';
var exports = {},method;
// Plugin Definition
var plugin = {
name:'<PluginName>',
api:{
'<methodName>':function(success,failure,options){
return {
success:success,
failure:failure,
params:[
options.foo,
options.bar,
options.baz
]
};
}
}
};
// Generates Cordova Plugin API
for (method in plugin.api) {
if (!plugin.api.hasOwnProperty(method)){continue;}
exports[method] = (function(name,setup){
return function() {
var config = setup ? setup.apply(this,arguments) || {} : {};
cordova.exec(
config.success || null,
config.failure || null,
plugin.name,
name,
config.params || []
);
};
}(method,plugin.api[method]));
}
// expose
if (!window.plugins) {window.plugins = {};}
window.plugins[plugin.name.charAt(0).toLowerCase() + plugin.name.slice(1)] = exports;
}(window.cordova));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment