Alberto Lagna
Alberto Lagna

Reputation: 51

sencha touch2: saving data from callback into context

I'm developing a sencha touch 2 app that uses the phoneGap barcode scanner plugin.

In the controller I wrote a function (doScan) that handles the tap event of the scan view button. If I tap the scan button the app calls the barcode scanner plugin through window.plugins.barcodeScanner.scan.

Within the callback I would like to call the controller, to set up a variable, to save the result of the scan but anything I do I get the error

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

because the callback function is not able to access the context. How should I do to save the result of the scan into the app context? Thanks.

/**
 * 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) {

}

});

Upvotes: 1

Views: 224

Answers (1)

Ye Liu
Ye Liu

Reputation: 8976

Try this:

doScan: function(...) {
    var me = this;
    ....
    window.plugins.barcodeScanner.scan(function(result) {
        me.doScanSuccess(result); // 'me' refers to the controller instance
     }...)
}

Upvotes: 1

Related Questions