0

I am developing a PhoneGap application for Android OS. I want to my application to be designed as most extendable as possible. Therefor I am writing all my modules as plgins, save them in one map and then I would like to use them in my HTML page. I wrote this code in MyJS.js file:

var map = {};

// Allow jQuery to cash the cordova.js
$.ajaxSetup({  cache: true});

$.getScript("cordova-2.6.0.js",function(){

    var AccelerometerSensor = {
            accelJSONObj:cordova.require("cordova/plugin/Acceleration"),
            accelPGAPSens:cordova.require("cordova/plugin/accelerometer"),


            color:'#FF8C00',
            sensorID:'Accelerometer',


            // Flag indicates whether this sensor type is supported by the device or not.
            availability:null,
            isAvailable:function() {
                accelPGAPSens.getCurrentAcceleration(
                        function(x){availability = true;}, 
                        function(){availability = false;});                  
            },

       }
})
.done(function(script, textStatus) {

    map["Accelerometer"] = this.AccelerometerSensor;
    alert('done');
 })
.fail(function(jqxhr, settings, exception) {  
    alert('fail');
});

Now I want to call the isAvailable function, so I wrote this code:

map["Accelerometer"].isAvailable()

But I got a TypeError:

"cannot call method 'isAvalable' of at undefined..."

What am I doing wrong? can anyone please show me what I have to do?

Thanks!!!

1 Answer 1

2

You are using :-

map["Accelerometer"] = this.AccelerometerSensor;

But "this" in done is different context i.e. global.

var map = {}, AccelerometerSensor;

// Allow jQuery to cash the cordova.js
$.ajaxSetup({  cache: true});

$.getScript("cordova-2.6.0.js",function(){

    AccelerometerSensor = {
            accelJSONObj:cordova.require("cordova/plugin/Acceleration"),
            accelPGAPSens:cordova.require("cordova/plugin/accelerometer"),


            color:'#FF8C00',
            sensorID:'Accelerometer',


            // Flag indicates whether this sensor type is supported by the device or not.
            availability:null,
            isAvailable:function() {
                accelPGAPSens.getCurrentAcceleration(
                        function(x){availability = true;}, 
                        function(){availability = false;});                  
            },

       }
})
.done(function(script, textStatus) {

    map["Accelerometer"] = AccelerometerSensor;
    alert('done');
 })
.fail(function(jqxhr, settings, exception) {  
    alert('fail');
Sign up to request clarification or add additional context in comments.

2 Comments

it's working now! thanks!. Just one another question now I am getting an error about the using of accelPGAPSens variable at the isAvailable() function that its not defined - can you please help me? thanks!
Try to use this.accelPGAPSens

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.