I have a mobile application within which I am attempting to use the Cordova Barcode Scanner Plugin.
In the demo, all of the scanner functions are placed within an extended Kendo Observable Object (kendo.data.ObservableObject.extend). I am able to get the scanner to work within my app and have deleted the extraneous code I do not need, but my ability to customize stops there. Here is the code that I am left with:
var ScanViewModel,
scanResult;
ScanViewModel = kendo.data.ObservableObject.extend({
scan: function () {
cordova.plugins.barcodeScanner.scan(
// success callback function
function (result) {
alert("Result: " + result.text);
scanResult = result.text;
},
// error callback function
function (error) {
alert("Scanning failed: " + error);
},
// options objects
{
"preferFrontCamera": false, // default false
"showFlipCameraButton": false // default false
}
);
}
});
app.scanBarcode = {
viewModel = new ScanViewModel()
}
What I want to be able to do is access results.text and assign it to a variable that I can then use throughout the rest of the app. As you can see, I have attempted to do this with the scanResult variable, but every time I go to use it after the scan, it comes back as undefined. I understand that this has something to do with it being an asynchronous call, but I'm not sure what the fix is. What can I do to get the value of results.text?
Also, is it necessary to place the scanner functionality within the extended Observable Object? I have tried every which way to access the scan function of the plugin, but this code seems to be the only way that works for me.
Any help would be appreciated. Thanks in advance!