Skip to Content
1
Apr 10, 2018 at 02:28 PM

Losing “This” context in navigator.camera.getPicture

574 Views Last edit May 27, 2020 at 07:32 AM 2 rev

How can the SAPUI5 Cordova function navigator.camera.getPicture pass in the current context?

For example I'm passing "this" into this.onImageCapture, which is passed in as the variable "a" - fine so far...

However, when navigator.camera.getPicture is called, only imageData is displayed.

If I add any parameters to it. E.g.

<code>navigator.camera.getPicture(this.onImageSuccess(a)......

I don't get the imageData passed in.

Also, I can't attach a promise to the function as .then can't be added to navigator.camera.getPicture either.

How do I access the current context (this) AND the imageData?

<code>handleMenuItemPress: function(oEvent) {

        var msg = "";

        msg = oEvent.getParameter("item").getValue();
        if (msg === "Camera") {
            this.onImageCapture(this);
        } 
        else if (msg === "Gallery") {
                this.onImageSelect(this);
        }
    }

onImageCapture: function(a) {
        var self = a;
        navigator.camera.getPicture(this.onImageSuccess, this.onImageFail, {
            //quality: 75,
            quality: 10,
            targetWidth: 300,
            targetHeight: 300,
            sourceType: navigator.camera.PictureSourceType.CAMERA,
            destinationType: navigator.camera.DestinationType.FILE_URI
        });
    },

onImageSuccess: function(imageData) {
    // can't access this
    },