[英]How to know if callback function used is same in Cordova?
I am trying to develop a cordova plugin for my android library. 我正在尝试为我的Android库开发一个cordova插件。 Below is a part of MyCordovaPlugin.js file:
以下是MyCordovaPlugin.js文件的一部分:
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. 如上所述,我有两个方法doA和doB,它们有两个参数-message和successCallback。
In my native java code, I need to know if the successCallback used in doA and doB is same or not. 在我的本地Java代码中,我需要知道doA和doB中使用的successCallback是否相同。 How can I do this?
我怎样才能做到这一点? How can I map a javascript function so that I can check if it's been used again?
如何映射javascript函数,以便检查是否再次使用过?
I have callbackContext value in my native java code. 我的本地Java代码中有callbackContext值。 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. 由于在本机层仅传递了随机生成的ID,该ID用于调用正确的Javascript回调函数,因此无法在本机级别上将一个Javascript函数与另一个Javascript函数区分开。
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.