Show TOC

Step 15: Nested ViewsLocate this document in the navigation structure

Our panel content is getting more and more complex and now it is time to move the panel content to a separate view. With that approach, the application structure is much easier to understand, and the individual parts of the app can be reused.

Preview
Figure 1: The panel content is now refactored to a separate view (No visual changes to last step)
Coding

You can view and download all files in the Explored app in the Demo Kit under Walkthrough - Step 15.

webapp/view/App.view.xml
<mvc:View
   controllerName="sap.ui.demo.wt.controller.App"
   xmlns="sap.m"
   xmlns:mvc="sap.ui.core.mvc"
   displayBlock="true" >
   <App class="sapUiDemoWT">
      <pages>
         <Page title="{i18n>homePageTitle}">
            <content>
               <mvc:XMLView viewName="sap.ui.demo.wt.view.HelloPanel"/>
            </content>
         </Page>
      </pages>
   </App>
</mvc:View>

Instead of putting the panel and its content directly into our App view, we will move it to a new separate HelloPanel view. We refer to this using an XMLView tag in the content aggregation of the panel.

webapp/view/HelloPanel.view.xml (New)
<mvc:View
   controllerName="sap.ui.demo.wt.controller.HelloPanel"
   xmlns="sap.m"
   xmlns:mvc="sap.ui.core.mvc">
   <Panel
      headerText="{i18n>helloPanelTitle}"
      class="sapUiResponsiveMargin"
      width="auto" >
      <content>
         <Button
            text="{i18n>showHelloButtonText}"
            press="onShowHello"
            class="myAppDemoWT myCustomButton"/>
         <Input
            value="{/recipient/name}"
            valueLiveUpdate="true"
            width="60%"/>
         <Text
            text="Hello {/recipient/name}"
            class="sapUiSmallMargin sapThemeHighlight-asColor myCustomText"/>
      </content>
   </Panel>
</mvc:View>

The whole content for the panel is now added to the new file HelloPanel.view.xml. We also specify the controller for the view by setting the controllerName attribute of the XML view.

webapp/controller/HelloPanel.controller.js (New)
sap.ui.define([
   "sap/ui/core/mvc/Controller",
   "sap/m/MessageToast"
], function (Controller, MessageToast) {
   "use strict";
   return Controller.extend("sap.ui.demo.wt.controller.HelloPanel", {
      onShowHello : function () {
         // read msg from i18n model
         var oBundle = this.getView().getModel("i18n").getResourceBundle();
         var sRecipient = this.getView().getModel().getProperty("/recipient/name");
         var sMsg = oBundle.getText("helloMsg", [sRecipient]);
         // show message
         MessageToast.show(sMsg);
      }
   });
});

To have a reusable asset, the method onShowHello is also moved from the app controller to the HelloPanel controller.

webapp/controller/App.controller.js
sap.ui.define([
   "sap/ui/core/mvc/Controller"
], function (Controller) {
   "use strict";
   return Controller.extend("sap.ui.demo.wt.controller.App", {
   });
});

We have now moved everything out of the app view and controller. The app controller remains an empty stub for now, we will use it later to add more functionality.