【发布时间】: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