cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with eventhandler and this

Former Member
0 Kudos

Hi

I got a problem with "this" in an eventhandler, when i create a new entity using the odata.v2 model.

I wan´t to call a method on another controller, when the update is successful.

oModel.create('/data', oEntry, {success: this._oListController.createOk});

The this._oListController.createOk method is called, but in the method "this" point to the window object. How can i change the reference to another type?

Kind regards,

Kim

Accepted Solutions (1)

Accepted Solutions (1)

former_member182372
Active Contributor
0 Kudos

oModel.create('/data', oEntry, {success: $.proxy(this._oListController.createOk, this._oListController.) } );

Former Member
0 Kudos

Hi Maksim

That works - thanks a lot!

Kind regards,

Kim

Answers (3)

Answers (3)

former_member183518
Active Participant
0 Kudos

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind

There are many ways to do it. Adding to the answers above, you can make use of javascript's bind method to pass *this* context to the function.


{success:this._oListController.createOk.bind(this)};

saivellanki
Active Contributor
0 Kudos

Hi Kim,

Did you try like this as well?


var that = this._oListController;

oModel.create('/data', oEntry, {success: function(){

  that.createOk;

  }});

Regards,

Sai Vellanki.

former_member182862
Active Contributor
0 Kudos

Hi Kim

Have you try this?

{success: [this._oListController.createOk, this._oListController]}

Thanks

-D

Former Member
0 Kudos

Hi Dennis

Thanks for your reply, but it doesn´t work. I get this error:

Uncaught TypeError: fnSuccess is not a function datajs-dbg.js:1637

fnSuccess is the reference to my callback method in the create method of the odata model:


fnSuccess  = mParameters.success;


Kind regards,

Kim

former_member182372
Active Contributor
0 Kudos

Dennis, i think it only works in js view for controls events, not the model