red_sensor
red_sensor

Reputation: 13

Phonegap plugin error

I`m trying to build a flashlight app, but it did not work on my device, in Firebug I have strange error:

ReferenceError: cordova is not defined 
cordova.addConstructor(Flashlight.install);

I use https://build.phonegap.com/plugins/294 plugin

My code index.html

<div id="button2" class="button1">Click!</div>

My code flashlightexec.js

$("#button2").live('click', function(){
    window.plugins.flashlight.available(function(isAvailable) {
      if (isAvailable) {

        window.plugins.flashlight.switchOn();
        alert("ok");

      } else {
        alert("Flashlight not available on this device");
      }
    });
});

Plugin code:

function Flashlight() {
  // track flashlight state
  this._isSwitchedOn = false;
}

Flashlight.prototype = {

  available: function (callback) {
    cordova.exec(function (avail) {
      callback(avail ? true : false);
    }, null, "Flashlight", "available", []);
  },

  switchOn: function (successCallback, errorCallback) {
    this._isSwitchedOn = true;
    cordova.exec(successCallback, errorCallback, "Flashlight", "switchOn", []);
  },

  switchOff: function (successCallback, errorCallback) {
    this._isSwitchedOn = false;
    cordova.exec(successCallback, errorCallback, "Flashlight", "switchOff", []);
  },

  toggle: function (successCallback, errorCallback) {
    if (this._isSwitchedOn) {
      this.switchOff(successCallback, errorCallback);
    } else {
      this.switchOn(successCallback, errorCallback);
    }
  }
};

Flashlight.install = function () {
  if (!window.plugins) {
    window.plugins = {};
  }

  window.plugins.flashlight = new Flashlight();
  return window.plugins.flashlight;
};

cordova.addConstructor(Flashlight.install);

What should I change to make flashlight works? Maybe there is another solution&

Upvotes: 0

Views: 790

Answers (1)

Eddy Verbruggen
Eddy Verbruggen

Reputation: 3550

I think you need to add the reference to cordova.js in your index.html before including the plugin .js file. Also, wait for the deviceready event to fire.

Upvotes: 1

Related Questions