我尝试创建一个SAPUI5 / OpenUI5应用程序。为此,我使用了一些XML视图,并在路由器之间进行导航。现在,我希望每次打开特定视图时调用方法。在阅读了onBeforeRendering
方法解决了这个案例后,我实现了这个功能。当我第一次导航到视图时,使用了该方法,但在第二次调用中没有。
这里是View-Controller的代码:
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/json/JSONModel"],
function(Controller , JSONModel) {"use strict";
return Controller.extend("Spellcheck.controller.Result", {
onBeforeRendering: function() {
this.model = new sap.ui.model.json.JSONModel({
suggestionData: []
});
this.getView().setModel(this.model);
this.model.refresh();
this.getCorrections();
},
getCorrections : function() {
//...some other code...
}
我希望有人知道我的问题的原因和/或合适的解决方案
这取决于视图周围的控件。更具体地说,仅当需要完全重新生成DOM子树时(当先前从DOM中删除视图的树时)才调用onBeforeRendering
/ onAfterRendering
。
我建议采用不同的方法,因为onBeforeRendering
通常应该用于与DOM /控件相关的事情。对于您的特定用例,最好听一下该路线的patternMatched
事件。这是UI5中最常见的做法。
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/json/JSONModel"
], function(Controller, JSONModel) {
"use strict";
return Controller.extend("Spellcheck.controller.Result", {
onInit: function() {
// It's better to use the JSONModel that you obtain from the
// enclosing function's parameters. This is because you are
// not using globals (so you are not coupled with globals).
// Also, you don't need to create a Controller property for the
// model; you can always get it with this.getView().getModel().
this.getView().setModel(new JSONModel({
suggestionData: []
});
// Refreshing the model immediately after making it does
// not do anything. You need to refresh it only if you
// change the data object from outside the model.
// this.model.refresh();
// Obtain the router, retrieve your route (replace myRoute
// with your route's name) and then attach a listener
// to the patternMatched event.
this.getOwnerComponent().getRouter().getRoute("myRoute")
.attachPatternMatched(this.getCorrections, this);
},
getCorrections : function() {
//...some other code...
}
}