1
votes

The SAPUI5 Developer Guide for SAP HANA (SPS 10) explains in "1.2.2.3.2 Add a Control to Your View" how to add a Listener to a Button (JS View):

var aControls = [];
var oButton = new sap.ui.commons.Button({
id : this.createId("MyButton"),
   text : "Hello JS View"
});
aControls.push(oButton.attachPress(oController.doIt));
return aControls;

And how to implement the controller:

doIt : function(oEvent) { alert(oEvent.getSource().getId() + " does it!"); }    

Unfortunately the code is not working in our system (SAP HANA SPS 09)

Which is the correct Code using MVC (not model, view, controll in one file)?
Where can I get correct developer information?

2

2 Answers

1
votes

You can also directly add the eventhandler in the button declaration:

new sap.m.Button("button12345", {  
   text : "call function" 
   press : oController.myTestFunction
});
0
votes

I figured out how to add a Listener to a button heeding the MVC concept:

View:

createContent : function(oController) {
   var btn = new sap.m.Button("button12345", {  text : "call function" });
   btn.attachPress(null, oController.myTestFunction, null);
   return new sap.m.Page({
        title : "Title",
        content : [btn]
   });
}

Controller:

myTestFunction : function() { alert("Successfully called the test function");}