cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing to function in View Content from Controller SAPUI5

mrvila1
Explorer
0 Kudos

Hi,

Can I accessing to function in View Content from Controller ?

From controller I want to call openDialog()


createContent : function(oController) {

     function openDialog() {

           alert("Ok!");

     }

}

Thank you

Accepted Solutions (1)

Accepted Solutions (1)

former_member195440
Participant
0 Kudos

Hi,

This is definitely not what you want to do to follow best practices for MVC concepts as Robin stated. The open dialog function should be placed in the controller and then the view calls if from there. I feel there is more to your question though.

It is also important to understand how you could do this, since there could be different situations that need something similar and for general understanding of javascript.

This is not possible how you have written your sample code. The function "openDialog" is only visible in the scope of the createContent function and cannot be called from outside. You could expose it in numerous ways, one being:


createContent : function(oController) { 

  

  // Creates a new function on the "this" object that can be called by anyone who has a reference to "this"

// Note that this function only exists after the "createContent" method is called so it is not safe to assume it exists all the time

     this.openDialog = function() { 

           alert("Ok!"); 

     };

}

// In the controller

var view = this.getView();

if (typeof view.openDialog === "function") {

      this.getView().openDialog();

}

It would be best that the function be in the controller, or if it is not just for one controller, can be placed in a utility or dedicated external object?

Oli

Answers (1)

Answers (1)

Qualiture
Active Contributor
0 Kudos

I don't think you can, but more importantly, you shouldn't either.

According to true MVC principles, a view should not contain any logic, so a view can (should) only access functions from a controller.

To solve your issue, simply move the function from the view to the controller and call it from there