SAPUI5对话问题
我的sapui5应用程序中有一个对话框,当选择“是”选项时,用户将进入主视图,但它表示我的功能未定义。我是否正确地从对话框中调用函数 这是我的密码: 加载主视图功能:SAPUI5对话问题,sapui5,hana,Sapui5,Hana,我的sapui5应用程序中有一个对话框,当选择“是”选项时,用户将进入主视图,但它表示我的功能未定义。我是否正确地从对话框中调用函数 这是我的密码: 加载主视图功能: loadHome : function() { this.router = sap.ui.core.UIComponent .getRouterFor(this); this.router.nav
loadHome : function() {
this.router = sap.ui.core.UIComponent
.getRouterFor(this);
this.router.navTo("HomePage");
},
我的对话:
cancelDialog : function() {
var oDialog1 = new sap.ui.commons.Dialog();
oDialog1.setTitle("Cancel Case");
var oText = new sap.ui.commons.TextView(
{
text : "Are you sure you want to cancel? Case data will not be saved"
});
oDialog1.addContent(oText);
oDialog1.addButton(new sap.ui.commons.Button({
text : "Yes",
press : function(){
this.loadHome();
}
}));
oDialog1.addButton(new sap.ui.commons.Button({
text : "No",
press : function() {
oDialog1.close();
}
}));
oDialog1.open();
},
这两个函数都与创建控制器一起使用。感谢您的帮助问题出在“是”按钮的事件处理程序中。在这个处理程序中,“this”指向被按下的按钮,而不是控制器。要获取对控制器的引用,只需调用bind(this)。或者,您也可以只在处理程序外部存储对控制器的引用,然后访问它(“var that=this;”等等…=>closures…” 是bind的一个工作示例(如下所示): 这是另一种方法(即=此…):
除此之外,您应该考虑使用SAP.M控件,而不是SAP.U.CuMon。然后还要考虑在对话框中使用片段,这将使代码更易于阅读+更好地重用对话框。。。我已经发布了一个很好的模板,给你和想法。也许你也想检查一下……
我认为Nabi的答案已经非常好和清楚了。但是,请记住,在有限的情况下,您可以通过id获取对象。比如说var oController = sap.ui.getCore().byId("id2").getController()
将为您获取对id2视图的控制器的引用。但请不要滥用此方法,因为一般来说,只访问this指针给定的有限函数范围,而不是应用程序中的所有对象是一件好事。非常感谢您的帮助,它工作得非常好。我一定会看看你的教程,谢谢
//...
var that = this;
oDialog1.addButton(
new sap.ui.commons.Button({
text : "Yes",
press : function(){
that.loadHome();
}
})
);
//...
var oController = sap.ui.getCore().byId("id2").getController()