温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - what is th proper way to refresh a full page on SAPUI5 (ESLINT:(sap-no-location-reload))?
eslint javascript refresh sapui5 page-refresh

javascript - 在SAPUI5(ESLINT:(sap-no-location-reload))上刷新整个页面的正确方法是什么?

发布于 2020-04-20 17:11:13

环境 :

框架:SAPUI5
版本:1.65.6
IDE:网络

问题:

我的代码可以工作,但是根据IDE可能不正确,Web-Ide返回错误:

location.reload()不允许。(sap-no-location-reload)
[ESLINT:(sap-no-location-reload)]

代码(最少复制):

控制器:

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel"
], function (Controller, JSONModel) {
    "use strict";

    return Controller.extend("namespace.controller.App", {
        onInit: function () {
            this.getView().setModel(new JSONModel({ 
                    actualizationDate: new Date()
                    }), "frontEnd");
        },
        onPressRefresh: function(){
            location.reload();
        }
    });
});

和视图:

<mvc:View 
    controllerName="namespace.controller.App" 
    xmlns:core="sap.ui.core"
    xmlns:mvc="sap.ui.core.mvc"
    xmlns="sap.m">
    <Shell id="shell">
        <App id="app">
            <pages>
                <Page id="page" title="{i18n>title}">
                    <content>
                        <Label text="{i18n>lastActualizedDateTime}" labelFor="lastActualizedTime" class="sapUiSmallMarginEnd"/>
                        <DateTimePicker 
                            id="lastActualizedTime" 
                            value="{path: 'frontEnd>/actualizationDate'}" 
                                    valueFormat="yyyy-MM-dd HH:mm:ss" 
                                    displayFormat="yyyy-MM-dd HH:mm:ss" 
                                    enabled="false"/>
                        <Button icon="sap-icon://refresh" type="Transparent" press="onPressRefresh"/>
                    </content>
                </Page>
            </pages>
        </App>
    </Shell>
</mvc:View>

为了重现,您可以直接从SAPUI5应用程序中复制WebIde创建模板中的代码。

备注:

  • 我知道代码可以正常使用location.reload();,我想知道它们是否是在SAPUI5中执行相同操作的正确方法(通过正确的方法,我的意思是不返回ESLINT错误)
  • location.reload(); 在控制器上的onPressRefresh中,它们仅是更多代码,您可以通过在Web端通过简单的复制来重现问题

查看更多

提问者
SylwekFr
被浏览
110
Samleijenhorst 2020-02-06 17:12

这样做的原因是完全不进行刷新是正确的方法。如果您想忽略该错误,

location.reload(); // eslint-disable-line sap-no-location-reload

但是,如果刷新的唯一原因是刷新日期,请按以下方式重写:

    sap.ui.define([
        "sap/ui/core/mvc/Controller",
        "sap/ui/model/json/JSONModel"
    ], function (Controller, JSONModel) {
        "use strict";

        return Controller.extend("namespace.controller.App", {
            onInit: function () {
                this.getView().setModel(new JSONModel({ 
                        actualizationDate: new Date()
                        }), "frontEnd");
            },
            onPressRefresh: function(){
                this.refreshDate();
            },
            refreshDate: function(){
                const oModel = this.getView().getModel("frontEnd");
                oModel.setProperty("/actualizationDate", new Date();
                oModel.updateBindings();
            }
        });
    });