Skip to Content

Losing “This” context in navigator.camera.getPicture

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.

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?

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
    },
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Apr 10 at 02:42 PM

    Hi Adam,

    I also struggled with that when starting with javascript. One way would be to code the function directly instead of using a callback function.

    onImageCapture: function(a) {
            var self = a;
            navigator.camera.getPicture(
    		function(imageData) {
    			--> self will be accessible
    		}
    	, 
    	this.onImageFail, {
                //quality: 75,
                quality: 10,
                targetWidth: 300,
                targetHeight: 300,
                sourceType: navigator.camera.PictureSourceType.CAMERA,
                destinationType: navigator.camera.DestinationType.FILE_URI
          });
    },
    
    Add comment
    10|10000 characters needed characters exceeded

    • Can you paste more code please? How is your onImageCapture function called?

      Try removing the "view"

      From

       var test = oEvent.getSource().getBindingContext("view");

      to

       var test = oEvent.getSource().getBindingContext();

      This will however give you the binding context of the object that trigger the event.

      From what I understand I think you want to retrieve the binding context of the whole view, you can also do the following

      this.getView().getBindingContext();

      Or to retrieve the object of the binding context of the view

      this.getView().getBindingContext().getObject();
      
  • Apr 10 at 02:40 PM

    navigator.camera.getPicture(jquery.proxy(this.onImageSuccess,this), this.onImageFail, { //quality: 75, quality: 10, targetWidth: 300, targetHeight: 300, sourceType: navigator.camera.PictureSourceType.CAMERA, destinationType: navigator.camera.DestinationType.FILE_URI });

    Add comment
    10|10000 characters needed characters exceeded