Issue while getting data from one XML View Control

2019-09-17 01:09发布

问题:

I have to use the one xml view controller methods into another XML View controller. Here is an example (controller files only):

**view1.js**
sap.ui.controller("sap.ui.xml.view.View1", {
  onInit: function() {
    this.globalCount = 0;
    loadedView1 = this.getView(); //loadedViews is a global variable which is defined in component.js
  },
  increaseCount: function(){
   this.globalCount++;
  }
});

**view1.xml**
<core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
    xmlns="sap.m" xmlns:l="sap.ui.layout" xmlns:c="sap.ui.commons" xmlns:f="sap.ui.layout.form"
    controllerName="sap.ui.fisa.view.OrderDetails" xmlns:html="http://www.w3.org/1999/xhtml">
    <Page showNavButton="true"  >
     <content>
        <Label text="example text" />
     </content>
    </Page>

Second View:

**view2.js**
    sap.ui.controller("sap.ui.xml.view.View2", {
      onInit: function() {

         this.view2Count = loadedView1.getController().globalCount ;

      }
    });

    **view2.xml**
    <core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
        xmlns="sap.m" xmlns:l="sap.ui.layout" xmlns:c="sap.ui.commons" xmlns:f="sap.ui.layout.form"
        controllerName="sap.ui.xml.view.View2" xmlns:html="http://www.w3.org/1999/xhtml">
        <Page showNavButton="true"  >
         <content>
            <Label text="Second View" />
         </content>
        </Page>
</core:View>

I am able to get the data from the loaded view, but when I try to reload the view2 the value of loadedView1.getController() is null.

回答1:

Instantiate a global JSONModel in your component and store the view count there. The model can be made globally available by calling setModel in the component's onInit method.