Search code examples
androidcordovaphonegap-pluginsnative-code

Creating a Phonegab plugin results in call methid 'doAction' of undefined


I have a minor Problem. I'm trying to create a plugin to do some heavy work in an native plugin and also shipping around a limitation in the web sqlite implementation. (Database is limited to 5MB, but I need some more).

First of all here's the Main class that is intended to do the work:

package com.worker.api;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;

public class worker extends Plugin{
    public final String version = "1.0";
    public String callback;
    public worker(){

    }

    public PluginResult execute(String action, JSONArray args, String
                                callbackId) {
        this.callback = callbackId;
        PluginResult r = null;
        try{
            if(action.equals("connectDB")){

            }else if(action.equals("closeDB")){

            }else if(action.equals("query")){
                r = this.query();
            }else if(action.equals("insert")){
                r = this.insert();
            }else if(action.equals("update")){
                r = this.update();
            }else if(action.equals("delete")){
                r = this.delete();
            }else if(action.equals("syncronize")){
                r = this.syncronize();
            }else if(action.equals("getVersion")){
                r = this.getVersion();
            }else{
                r = new PluginResult(Status.INVALID_ACTION);
                r.setKeepCallback(true);
            }
        }catch(JSONException ex){
            r = new PluginResult(Status.JSON_EXCEPTION);
        }
        return r;
    }
    public PluginResult query(){
        return null;
    }
    public PluginResult insert(){
        return null;
    }
    public PluginResult update(){
        return null;
    }
    public PluginResult delete(){
        return null;
    }
    public PluginResult syncronize(){
        return null;
    }
    public PluginResult getVersion() throws JSONException{
        JSONObject obj = new JSONObject();
        obj.put(version,this.version);
        return new PluginResult(Status.OK,obj);
    }
}

At the moment I only implemented the getVersion part as I'm still doing testing on this

In the plugin.xml I added this:

<plugin name="workerAPI" value="com.worker.api.worker" />

The main js file looks like this:

    var workerApi = function(){}

    var genericSuccess = function(e) {
        alert("Success: " + e.toString());
    };

    var fail = function(e) {
        alert("Failure " + e.toString());
    };

    workerApi.prototype.doAction = function(
            operation,
            argument,
            successFunction,
            failFunction
            ){
        var succ = successFunction ? successFunction : genericSuccess;
        var faic = failFunction ? failFunction : fail;
        return PhoneGap.exec(
                succ,
                faic,
                "workerAPI",
                operation,
                [argument]
        );
    };

    PhoneGap.addConstructor(function() {
        PhoneGap.addPlugin(
                    "workApi",
                    new workerApi()
        );
    });

    var WKapi = {
            "openDB": function(){
                window.plugins.workApi.doAction(
                    "connectDB",
                    null
                );
            },
            "closeDB": function(){
                window.plugins.workApi.doAction(
                    "closeDB",
                    null
                );
            },
            "query": function(arg){
                window.plugins.workApi.doAction(
                    "query",
                    arg
                );
            },
            "insert": function(arg){
                window.plugins.workApi.doAction(
                    "insert",
                    arg
                );
            },
            "update": function(arg){
                window.plugins.workApi.doAction(
                    "update",
                    arg
                );
            },
            "delete": function(arg){
                window.plugins.workApi.doAction(
                    "delete",
                    arg
                );
            },
            "sync": function(arg){
                window.plugins.workApi.doAction(
                    "syncronize",
                    arg
                );
            },
            "version": function(){
                window.plugins.workApi.doAction(
                    "version"
                );
            }
    };

As soon as I execute it, the logger LogCat tells me as error:

Uncaught TypeError: Cannot call methid 'doAction' of undefined at file:///android_asset/www/worker.js:79

I don't really know what I'm doing wrong.

-----ADDON------

Here's the codeblock that is part of the index.php

        <script lang="text/javascript">
            document.addEventListener("deviceready", onDeviceReady, false);
            var isRun = false;
            function onDeviceReady() {
                WKapi.version();
            };
        </script>
    </div>
</body>
</html>

I still get Uncaught TypeError: Cannot call method 'doAction' ... It happens on this call: window.plugins.workerApi.doAction

If I put the window.plugins.workerApi into a alert it gives me unknown back.


Solution

  • In your Java class, you are looking for "getVersion", so that's what needs to be passed here.

    "version": function(){
        window.plugins.workApi.doAction(
            "getVersion"
            );
        }