This is a migrated thread and some comments may be shown as answers.

Barcode Scanner Confusion

2 Answers 56 Views
General Discussion
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Devon
Top achievements
Rank 1
Devon asked on 14 Jul 2016, 07:34 PM

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!

2 Answers, 1 is accepted

Sort by
0
Accepted
Preslav
Telerik team
answered on 19 Jul 2016, 12:20 PM
Hi Devon,

Thank you for your question.

I checked the code snippet and was able to reproduce the issue. I assume the problem comes from the fact that, the "scanResult" variable is not globally set. Due to this it is undefined when you call it. To fix this behavior you can try to set the variable globally in your .js file.

To guide you further I have prepared a sample app for which I used the Cordova BarcodeScanner Plugin. You can take a look at its index.html and scripts->demo.js files. In the index.html there is a new button with a callback to the "showScanResult" function and in the demo.js I've put the definition of the "showScanResult" function and the "scanResult" variable. I would suggest to test the app on a device and see what the "Show data in Scan Result!" button does.

Let me know if you have any questions or comments on that topic. I hope this helps. 
 
Regards,
Preslav
Telerik by Progress
 

Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

 
0
Devon
Top achievements
Rank 1
answered on 19 Jul 2016, 03:01 PM
Thank you!! This is exactly what I needed. Works perfectly.
Tags
General Discussion
Asked by
Devon
Top achievements
Rank 1
Answers by
Preslav
Telerik team
Devon
Top achievements
Rank 1
Share this question
or