I am trying to develop a cordova plugin for my android library. Below is a part of MyCordovaPlugin.js file:
var exec = require('cordova/exec');
module.exports = {
doA : function(message, successCallback) {
exec(successCallback,
null, // No failure callback
"MyCordovaPlugin",
"doA",
[message]);
},
doB : function(message, successCallback) {
exec(successCallback,
null, // No failure callback
"MyCordovaPlugin",
"doB",
[message]);
}
};
As mentioned above, I have two methods doA and doB which takes two arguments - message and successCallback.
In my native java code, I need to know if the successCallback used in doA and doB is same or not. How can I do this? How can I map a javascript function so that I can check if it's been used again?
I have callbackContext value in my native java code. But the value would be different when called.
It's not possible to distinguish at the native level one Javascript function from another, since the native layer is just passed a randomly generated ID which it uses to invoke the correct Javascript callback function.
So I would pass through an additional argument to your plugin methods which allows you to distiguish this at the native level.
Something like this:
myPlugin.js
var exec = require('cordova/exec');
module.exports = {
doA : function(message, successCallback, callbackName) {
exec(successCallback,
null, // No failure callback
"MyCordovaPlugin",
"doA",
[message, callbackName]);
},
doB : function(message, successCallback, callbackName) {
exec(successCallback,
null, // No failure callback
"MyCordovaPlugin",
"doB",
[message, callbackName]);
}
};
myApp.js
function foo(){
console.log("foo");
}
function bar(){
console.log("bar");
}
myPlugin.doA("Some message", foo, "foo");
myPlugin.doB("Some message", foo, "foo");
myPlugin.doA("Some message", bar, "bar");
myPlugin.doB("Some message", bar, "bar");
myPlugin.java
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
String message = args.getString(0);
String callbackName = args.getString(1);
// etc
}