Phonegap插件错误

时间:2013-12-05 21:55:59

标签: javascript jquery html phonegap-plugins phonegap-build

我正在尝试构建一个手电筒应用程序,但它在我的设备上无效,在Firebug中我有一个奇怪的错误:

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

我使用https://build.phonegap.com/plugins/294插件

我的代码index.html

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

我的代码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");
      }
    });
});

插件代码:

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);

我应该改变什么来使手电筒工作?也许还有另一种解决方案&amp;

1 个答案:

答案 0 :(得分:1)

我认为你需要在包含插件.js文件之前在index.html中添加对cordova.js的引用。另外,等待deviceready事件触发。