1

我正在开发一个使用 phoneGap 条码扫描器插件的 sencha touch 2 应用程序。

在控制器中,我编写了一个函数(doScan)来处理扫描视图按钮的点击事件。如果我点击扫描按钮,应用程序会通过 window.plugins.barcodeScanner.scan 调用条形码扫描仪插件。

在回调中,我想调用控制器,设置变量,保存扫描结果,但是我做的任何事情都会出错

Error in error callback: org.apache.cordova.barcodeScanner381646541 = TypeError: 'undefined' is not a function

因为回调函数无法访问上下文。我应该如何将扫描结果保存到应用程序上下文中?谢谢。

/**
 * Controller of the scan view
 */
Ext.define('MyApp.controller.ScanController', {
    extend: 'Ext.app.Controller',
    config: {
       refs: {
            scanButton: '#scanButton'
        },
        control: {
            scanButton: {
            tap: 'doScan'
        }
    }
    },

    /**
     * Scans a barcode
     */
    doScan: function(button, event) {

        window.plugins.barcodeScanner.scan(
            function(result) {
            if (result.cancelled){
                console.log("the user cancelled the scan")
            } else {
                console.log("scannerSuccess: result=" + result.text)
            // I'd like to call the controller here
            }
        },
        function(error) {
            console.log("scanning failed: " + error.text)
        }
    )
}, 

doScanSuccess: function(result) {

}

});
4

1 回答 1

1

尝试这个:

doScan: function(...) {
    var me = this;
    ....
    window.plugins.barcodeScanner.scan(function(result) {
        me.doScanSuccess(result); // 'me' refers to the controller instance
     }...)
}
于 2013-06-07T16:11:48.843 回答