未调用 onBeforeRendering 方法 [英] Method onBeforeRendering not called

查看:51
本文介绍了未调用 onBeforeRendering 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个 SAPUI5/OpenUI5 应用程序.为此,我使用了一些 XML 视图并使用路由器在它们之间导航.现在,我想在每次打开特定视图时调用一个方法.看到方法 onBeforeRendering 解决了这种情况后,我实现了这个功能.当我第一次导航到视图时,使用了该方法,但没有在第二次调用中使用.

I try to create a SAPUI5/OpenUI5 application. For that I use some XML-views and navigate between this with a router. Now, I want to call a method every time a specific view is opened. After reading that the method onBeforeRendering solves this case, I implement this function. When I navigate first time to the view the method was used, but not in the second call.

这里是视图控制器的代码:

Here the code of the 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...
    }

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

I hope someone know the reason and/or suitable solution for my problem

推荐答案

这取决于视图周围的控件.更具体地说,onBeforeRendering/onAfterRendering 仅在 DOM 子树需要完全重新生成时(当视图的树之前从 DOM 中移除时)才会被调用.

It depends on the controls that you have around the view. More specifically, onBeforeRendering/onAfterRendering is called only when the DOM sub-tree needs to be completely regenerated (when the view's tree was removed previously from the DOM).

我会提出一种不同的方法,因为 onBeforeRendering 通常应该用于与 DOM/控件相关的事情.对于您的特定用例,最好监听路由的 patternMatched 事件.这是 UI5 中最常见的做法.

I would propose a different approach because the onBeforeRendering should generally be used for things related to the DOM / controls. For your specific use case, it would be better to listen on the patternMatched event of the route. This is the most common practice in 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...
    }
}

这篇关于未调用 onBeforeRendering 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆