简体   繁体   English

如何知道在Cordova中使用的回调函数是否相同?

[英]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.

相关问题 如何知道触发了回调函数? - How to know that a callback function has fired? 如何编写要在Java回调上使用或不使用回调的函数? - How to code a function to be used with or without callback on Javascript? Cordova / Javascript:方法作为回调函数 - Cordova/Javascript: method as callback function 如果在两个不同的插件中使用了Cordova onCordovaInfoReady,该如何处理? - How to handle cordova onCordovaInfoReady if the same is used in two different plugins? JavaScript如何知道Promises回调函数何时可以执行? - How does JavaScript know when a Promises callback function is ready to be executed? 我们如何知道在YUI中的回调函数中传递了哪些参数? - How can we know what parameters are passed into a callback function in YUI? 有谁知道如何为jQuery cycle插件设置回调函数? - Does anyone know how to set a callback function for the jQuery cycle plugin? 在for循环中设置标志,以在同一循环中调用的函数的回调函数中使用 - Setting a flag in a for loop to be used in the callback function of a function called within the same loop 如何避免在用作快速路由回调的函数中出现“此”问题 - how to avoid 'this' issue in function used as express route callback 如何从自定义的forEach函数获取返回的值以用于回调? - How to get the value from customised forEach function returned to be used in callback?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM