如何从外部访问组件模型
How to access component model from outside
我在 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
// ...
}
我在 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
// ...
}