Callback Functions inside the Cordova.exec with TypeScript
i have a problem with TypeScript Definition for Cordova.
The codrova.d.ts file allows for success-callback and error-callback no function arguments.
for a better understanding of my problem a little example:
Thats the original code of the cordova.d.ts file
exec(success: () => any, fail: () => any, service: string, action: string, args?: string[]): void;
now i want to write a class in typescript like this structure
module Plugin {
export class {
constructor(){
cordova.exec(this.success,this.error,"Service","Action",null);
}
private success(message:string)
{
//do Something
}
private error(message:string)
{
//do Something
}
Now i got a error in the line of the cordova.exec call with the message, that the function signature of success and error match like this (message:string)=>void and ()=>any are not compatible.
My Question is, how can i use callback arguments to give a more detailed information to the platform independend side if a call was successfull or not.
Or is it a mistake inside the cordova.d.ts operation?
Thanks for help!
Quick Fixes
There are a few fixes you need to your example code...
module Example {
class Test {
constructor() {
cordova.exec(this.success, this.error, "Service", "Action", null);
}
private success() {
//do Something
}
private error() {
//do Something
}
}
}
This passes the definition for Cordova.
If you suspect this is wrong and both success
and error
may actually take a string, you could make them optional and it would still pass the existing type check:
private success(message?: string) {
//do Something
}
Proper Fixes
Of course, if the type definition is wrong, you can update it with the correct definitions and submit it back to the Definitely Typed project...
exec(success: Function,
fail: (message: string) => any,
service: string,
action: string,
args?: string[]): void;
I have different suggested fixes here, based on an assumption... the success function will be passed something specific on success, perhaps a message or an object (who knows what). The fail function will be passed a string message.
链接地址: http://www.djcxy.com/p/64394.html