【问题标题】:SAPUI5: How to show a loading dialog box while pushing data to OData Service?SAPUI5:将数据推送到 OData 服务时如何显示加载对话框?
【发布时间】:2017-11-20 14:27:20
【问题描述】:

我正在构建一个新的 SAPUI5 应用程序,而不采用任何模板方法。我正在构建的只是一个带有两个字段和一个按钮的小表单。啊……“按钮”。

那个按钮呢?按钮代码如下:

<Button text="OK" width="100%" id="__button1" press="insertIntoOData"/>

这样,我希望当我按下按钮时,会调用 insertIntoOData 函数。你猜怎么着!?是的!

太棒了!

但问题是,在 insertIntoOData 中,我希望它在 OData 模型处理记录的插入时显示一个对话框(由片段构建 - 检查此 link)。不幸的是,我还没有设法让对话框显示出来。看起来 insertIntoOData 函数是同步调用的,并且在函数完成之前不会显示对话框。

当 OData 模型完成插入处理后,将处理响应并且对话框仅显示片刻,因为您可能会在 insertIntoOData 的以下代码中注意到,导航被重定向到主(主)页面。

insertIntoOData: function(evt) {

    /* 
    * to prevent namespace issues, reserve 'this' into 'that',
    * so the ajax will know who to call inside its scope
    */
    var that = this;

    //declare the dialog
    if (!that._dialog) {
        that._dialog = sap.ui.xmlfragment("valac.view.requestloading", null);
        that.getView().addDependent(that._dialog);
    }

    // open dialog
    jQuery.sap.syncStyleClass("sapUiSizeCompact", that.getView(), that._dialog);
    that._dialog.open();


    // get the csrf token from the service url
    var csrfToken = this.getCSRFToken("/valacDestination/sap/c4c/odata/v1/c4codata/ValacObjectCollection");

    // get the values from the 'form'
    var name_var = this.byId("tSubjectInput").getValue();
    var description_var = this.byId("tDescriptionArea").getValue();

    // create the entry that will be sent with the request
    var oEntry = {};

    // the description list
    oEntry.requestDescription = [];

    // the description item that goes inside the list
    var entryOfRequestDescription = {};
    entryOfRequestDescription.Text = description_var;
    oEntry.requestDescription.push(entryOfRequestDescription);

    // name is a complex object that needs to be built. Content and language.
    oEntry.Name = {};
    oEntry.Name.content = name_var;
    oEntry.Name.languageCode = "EN";

    // fetch the model schema
    var oModel = new sap.ui.model.odata.ODataModel("/valacDestination/sap/c4c/odata/v1/c4codata/");

    sap.ui.getCore().setModel(oModel);

    /* create the entry into the model schema via ajax
    * return to the master page if there's a success response
    * put a message on the master page.
    */
    oModel.create('/ValacObjectCollection', oEntry, null, function(response){
        that._dialog.close();
        sap.ui.core.UIComponent.getRouterFor(that).navTo("master");     
        sap.m.MessageToast.show("Object Persisted!", {
            duration: 30000000
        });
    },function(){
        that._dialog.close();
        sap.m.MessageToast.show("ERROR!", {
            duration: 30000000
        });
    });
}

我的问题是:如何在 insertIntoOData 结束或调用 oModel.create 函数之前显示对话框?

【问题讨论】:

  • 好吧,如果您的请求处理得非常快,那么显示对话框真的有意义吗?视图(和控件)具有 busy 属性,这可能更适合您的情况。
  • 嗨@Valak,使odata请求异步。默认情况下,创建请求是同步的,因此浏览器正在等待响应到达,然后继续工作。因此,一旦响应返回,它就会打开弹出窗口,但下一行会说:关闭弹出窗口。因此,您会看到第二个弹出窗口。 TL;DR:JS 是单线程语言。使用 oData.create 的 async 属性并检查。
  • @Marc:请求很慢,这就是我想要加载东西的原因。视图的 busy 属性也不做任何事情。
  • @RahulBhardwaj:感谢您的回答。我已经检查过了,因为默认情况下 oData.create 是异步的。我尝试了显式异步属性的方法。没啥事儿。一切都保持不变。
  • @Valac:在 odata.create 中是异步的,默认为 false。您能否更新代码,以便我们在同一页面上。 async : true 必须设置。

标签: javascript ajax dialog modal-dialog sapui5


【解决方案1】:

当您输入 insertIntoOData 方法时。 在调用服务集之前

  that._dialog.setBusy(true);

获得服务响应后(成功或错误无关紧要)设置为

that._dialog.setBusy(false);

【讨论】:

    【解决方案2】:

    你可以做全局忙指示或组件忙指示,显示在oModel.create之前并隐藏到成功或错误函数中:

    sap.ui.core.BusyIndicator.show(0); <- Parameter is delay time.
    
    sap.ui.core.BusyIndicator.hide();  <- hide
    

    链接文档:https://sapui5.hana.ondemand.com/1.44.18/explored.html#/sample/sap.ui.core.sample.BusyIndicator/preview

    只有对话框显示忙。

    that._dialog.setBusy(true); <- Show
    
    that._dialog.setBusy(false); <- hide
    

    【讨论】:

    • 感谢您的回答。我已经尝试过这种方法,但只有在 odata 服务发送响应后,屏幕才会变得忙碌。当它发生时,屏幕“闪烁”(即,它变得忙碌,然后转眼间不忙碌)并导航回母版页。我猜这与我使用的是 sapui5 fiori 主明细模板有关,而我的代码在明细控制器中。
    【解决方案3】:

    我已经成功展示了busyIndi​​cator。 我将 insertIntoOData 函数重建为如下:

        insertServiceRequestIntoOData: function(evt) {
    
            var that = this;
            var token = null;
            var serviceUrl = "URL";
    
            var name_var = this.byId("tSubjectInput").getValue();
            var description_var = this.byId("tDescriptionArea").getValue();
    
            var oEntry = {};
            /*
             * oEntry building process omitted
             */
    
            this.oModel = new sap.ui.model.odata.ODataModel(serviceUrl);
            sap.ui.getCore().setModel(this.oModel);
    
            /*
             * This is where the magic happens:
             * 1) ajax async request to get the token and to show the busy indicator on the screen
             * 2) when it's over, make a post to the oData service with the data.
             * 3) when it's over, hide the busy indicator and go to the correct page (success or error).
             */
            $.ajax({
                url: serviceUrl + "/MyCollection";
                type: "GET",
                async: true,
                beforeSend: function(xhr) {
                    sap.ui.core.BusyIndicator.show(0);
                    xhr.setRequestHeader("X-CSRF-Token", "Fetch");
                },
                complete: function(xhr) {
                    token = xhr.getResponseHeader("X-CSRF-Token");
    
                    // begin of odata send
                    that.oModel.create("/MyCollection", oEntry, null, function(response){
                        sap.ui.core.BusyIndicator.hide();
                        sap.ui.core.UIComponent.getRouterFor(that).navTo("insertConfirmation"); 
                        that.clearInputs();
    
                        },function(){
                            sap.ui.core.BusyIndicator.hide();
                            sap.ui.core.UIComponent.getRouterFor(that).navTo("insertErrorConfirmation");    
                            that.clearInputs();
                    });
    
                }
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多