在实现基本控制器时,我遇到了意外错误。当用户导航到视图时,以下代码可以正常工作(意味着创建bindingContext)。如果用户手动重新加载页面,则上下文保持未定义状态。我调试了代码,并且两种情况下的模型都是相同的对象。现在,我添加了手动检查以防止错误。问题是:为什么没有创建绑定上下文?
欢呼
sap.ui.define([
"de/nordakademie/bookatrip/controller/BaseController",
"sap/ui/core/routing/History",
"sap/m/MessageToast"],
function (BaseController, History, MessageToast) {
"use strict";
return BaseController.extend("de.nordakademie.bookatrip.controller.BaseDetail", {
onInit: function () {
this._getRouter()
.getRoute("detail")
.attachPatternMatched(this._onDetailRouteMatched, this);
},
_onDetailRouteMatched: function (oEvent) {
const oArgs = oEvent.getParameter("arguments");
const oView = this.getView();
const i18n = this.getI18nBundle();
const oContext = oView
.getModel("trip")
.createBindingContext("/" + oArgs.path);
/**
* in case of manual browser reload, binding the context will fail (why?)
* to prevent unexpected errors on tis view, the user will be navigated to
* previous page
*/
if (oContext) {
oView.setBindingContext(oContext, "trip");
} else {
MessageToast.show(i18n.getText("detail.binding.navback"),
{closeOnBrowserNavigation: false});
this.onNavButtonPress()
}
}
});
答案 0 :(得分:0)
您手动创建BindingContext是否有特定原因?通常,我们使用bindElement方法绑定视图。
使用bindElement方法时,如果重新加载页面,框架将负责重用现有的绑定(不是完全重新加载,而是相同路径的重新匹配)。
尝试这样操作:
_onDetailRouteMatched: function (oEvent) {
const oArgs = oEvent.getParameter("arguments");
const oView = this.getView();
const i18n = this.getI18nBundle();
oView.bindElement({
model: "trip",
path: "/" + oArgs.path
});
}
Obs:您也可以使用模型名称作为路径的一部分来绑定它,如下所示:
oView.bindElement("{trip>/" + oArgs.path + "}");