方法onBeforeRendering未调用

时间:2017-03-05 16:54:38

标签: sapui5

我尝试创建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...
    }

我希望有人知道我的问题的原因和/或合适的解决方案

1 个答案:

答案 0 :(得分:7)

这取决于视图周围的控件。更具体地,仅当需要完全重新生成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...
    }
}