如何从外部访问组件模型

问题描述 投票:0回答:1

我在 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()
返回
undefined
)工作了。我假设 AppShell 控制器没有所有者。但是我如何访问该控制器的
onInit
中的默认模型?

sapui5
1个回答
1
投票

您的案例中的应用程序结构有些不寻常 - 尽管如此,只要您可以访问内部组件,您始终可以访问

manifest.json
中定义的模型。

假设

this
引用
internal_app.view.AppShell
的控制器实例,你可以得到这样的默认模型:

onInit: async function() { // Element required from "sap/ui/core/Element"
  const mySapMShell = /*...*/;
  const component = await this.componentLoaded(mySapMShell.getApp());
  const myDefaultModel = component.getModel(); // model from manifest.json
  // ...
},

componentLoaded: function(componentContainer) {
  const component = componentContainer.getComponent();
  return component ? Promise.resolve(component) : new Promise(resolve => {
    componentContainer.attachEventOnce("componentCreated", event => {
      resolve(event.getParameter("component"));
    });
  });
},
© www.soinside.com 2019 - 2024. All rights reserved.