Hi guys. cordova-ios has a nice method binding for plugins. Unfortunately
cordova-android requires at present boilerplate implementation of method
execute:
public class MyPlugin extends CordovaPlugin {
...
@Override
public boolean execute(String action, JSONArray args, CallbackContext
callbackContext) throws JSONException {
if (METHOD_1.equals(action)) {
method1(args, callbackContext);
} else if (METHOD_2.equals(action)) {
method2(args, callbackContext);
...
} else {
return false;
}
return true;
}
...
}
I suggest to implement support for @CordovaMethod that will allow for plugin
authors to reduce boilerplate code, so the implementation above will look like:
public class MyPlugin extends CordovaPlugin {
...
@CordovaMethod
private void method1(JSONArray args, CallbackContext callbackContext)
throws JSONException {
...
}
@CordovaMethod
private void method2(JSONArray args, CallbackContext callbackContext)
throws JSONException {
...
}
...
}
Implementation of method execute in CordovaPlugin.java will check for methods
marked with @CordovaMethod and if action argument matches a method with
@CordovaMethod, the method will be invoked. Developer can also specify action
manually via the name parameter:
public class MyPlugin extends CordovaPlugin {
...
@CordovaMethod(name = "method1")
private void myMethod(JSONArray args, CallbackContext callbackContext)
throws JSONException {
...
}
...
}
Important to note that backward compatibility is preserved - old plugins didn't
have @CordovaMethod, but new ones can use it to simplify code.
What do you think?
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]