Skip to Content
1

Losing “This” context in navigator.camera.getPicture

Apr 10 at 02:28 PM

55

avatar image

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
    },
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
David Lapointe Apr 10 at 02:42 PM
1

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
      });
},
Show 2 Share
10 |10000 characters needed characters left characters exceeded

Works great thank you...

My next issue is I can't retreive the binding context

onImageCapture:function(a,oEvent){

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

"Test" returns undefined...

0

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();
0
Jun Wu Apr 10 at 02:40 PM
1

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 });

Share
10 |10000 characters needed characters left characters exceeded