sapui5 - 如何从外部访问组件模型
问题描述
我在index.html中创建了一个 shell-in-shell 构造:
sap.ui.getCore().attachInit(function () {
// create a new Shell that contains the root view
var oShell = new sap.m.Shell({
id: "appShell",
app: new sap.ui.core.ComponentContainer({
name: "internal_app",
height: "100%"
})
});
// load the view that contains the unified shell
var oAppShellView = sap.ui.view({
type: sap.ui.core.mvc.ViewType.XML,
viewName: "internal_app.view.AppShell"
});
// access the unified shell from the view
var oUnifiedShell = oAppShellView.byId("unifiedShell");
// place the app shell in the unified shell
oUnifiedShell.addContent(oShell);
oAppShellView.placeAt("content");
});
此外,manifest.json 中还定义了一个默认模型:
....
},
"models": {
"": {
"type": "sap.ui.model.json.JSONModel"
}
},
....
在视图的控制器internal_app.view.AppShell
(由上面的代码片段创建)中,我现在想访问默认模型,但也this.getModel()
没有this.getOwnerComponent().getModel()
(getModel()
和getOwnerComponent()
return undefined
)工作。我假设 AppShell 控制器没有所有者。但是如何访问该onInit
控制器的默认模型?
解决方案
您的案例中的应用程序结构有些不寻常 - 不过,您始终可以访问 manifest.json 中定义的模型,只要您可以访问内部组件。
假设this
正在引用 的控制器internal_app.view.AppShell
,您可以像这样获得默认模型:
onInit: function() {
var innerShell = sap.ui.getCore().byId("appShell"); // only if the app is standalone
this.componentLoaded(innerShell.getApp()).then(this.onComponentCreated.bind(this));
},
componentLoaded: function(componentContainer) {
var component = componentContainer.getComponent();
return component ? Promise.resolve(component) : new Promise(function(resolve) {
componentContainer.attachEventOnce("componentCreated", function(event) {
resolve(event.getParameter("component"));
}, this);
}.bind(this));
},
onComponentCreated: function(component) {
var myDefaultModel = component.getModel(); // model from manifest.json
// ...
}
推荐阅读
- scala - 为什么 SBT 在编译时会找到导入,但在构建项目时却找不到?
- javascript - 如果通过 html 属性订阅,为什么我的 Web 组件的 CustomEvent 事件处理程序没有被调用?
- javascript - 从php中检索数据
- kubernetes - google PD 的 kubernetes 卷挂载问题(已安装 kubeadm)
- sql - 连接多个表时,总和是它应该是的倍数
- vb.net - 使用 Runspace 在 Visual Basic 中调用 powershell 脚本时出现 System.NullReferenceException
- ruby - 大型 JSON 字符串中的字符串插值
- python - 当python中的一行中有多个最大值时,在2维向量的每一行中查找最大值
- java - 只有在其他片段被销毁时才启动一个方法
- javascript - 对于 JavaScript 的 toLocaleDateString,是否有替代 dateStyle 和 timeStyle 的新标准?