简体   繁体   English

Cordova(Ionic2)自定义插件:使用cordova.exec()successHandler管理Angular2新Promise()

[英]Cordova (Ionic2) custom plugin: manage Angular2 new Promise() with cordova.exec() successHandler

I am building a Cordova plugin to use with Ionic2 . 我正在构建一个与Ionic2一起使用的Cordova插件 I am kind of experienced with Cordova (and custom plugins) but rather new to Ionic2 (and hence also new at Angular2, TypeScript ). 我对Cordova(和自定义插件)很有经验,但对Ionic2很新(因此也是Angular2,TypeScript的新手 )。

(What I am asking now, I used to manage it with Jquery Deferred) (我现在要问的是,我曾经用Jquery Deferred来管理它)

In my "app.component.ts" file I have: 在我的“app.component.ts”文件中,我有:

import...

declare var MyPlugin: any;

@Component({
  templateUrl: 'app.html'
})

export class MyApp {
  ...
  initializeApp() {
    this.platform.ready().then(() => {
      StatusBar.styleDefault();
      MyPlugin.action();
    });
  }
  ...
}

I won't detail the "plugin.xml" file for my plugin. 我不会详细说明我的插件的“plugin.xml”文件。 This works fine. 这很好用。

The "MyPlugin.js" goes like that: “MyPlugin.js”就是这样的:

var PLUGIN_NAME = "MyPlugin";

var MyPlugin =function(){};

MyPlugin.action(){
    cordova.exec(
        successResultHandler,
        errorResultHandler,
        PLUGIN_NAME,
        action,
        []
    );
} 
exports.module=MyPlugin;

In my Java Class linked to "MyPlugin.js" file I have something like: 在链接到“MyPlugin.js”文件的Java类中,我有类似的东西:

public class MyPlugin extends CordovaPlugin {

    @Override
    public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
          //something async
          callbackContext.success()
    }
}

When the Java class triggers the callbackContext.success , the JS cordova.exec will launch the function successResultHandler . 当Java类触发callbackContext.success ,JS cordova.exec将启动函数successResultHandler I was wondering how to use the Angular2 Promise to catch the async event??? 我想知道如何使用Angular2 Promise来捕获异步事件???

I started to write some code but it doesn't make sense the order I should use. 我开始编写一些代码但是我应该使用的顺序没有意义。

I was thinking of doing something like that in "MyPlugin.js": 我想在“MyPlugin.js”中做类似的事情:

MyPlugin.action(){
   return new Promise(function(resolve,reject){
    cordova.exec(
        successResultHandler,
        errorResultHandler,
        PLUGIN_NAME,
        action,
        []
    );
    });
} 

But of course it doesn't make any sense, how do you catch the result of the function successResultHandler in resolve that way. 但是当然它没有任何意义,你怎么抓住函数successResultHandler的结果来resolve这个问题。

Has anyone an idea? 有人有想法吗?

The resolve and reject parameters that the Promise constructor passes into the callback function are themselves functions, so you should be able to do something like this: Promise构造函数传递给回调函数的resolve和reject参数本身就是函数,所以你应该能够做到这样的事情:

MyPlugin.action(){
   return new Promise((resolve,reject) => {
        cordova.exec(
            resolve,
            reject,
            PLUGIN_NAME,
            action,
            []
        );
    });
};

Then invoke it like this: 然后像这样调用它:

MyPlugin.action().then(() => {
    console.log("Success");
},(err) => {
    console.error(err);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM