从一个XML视图控制器获取数据到sapui5中的另一个视图控制器时出现问题

时间:2015-06-17 16:03:39

标签: xml sapui5 jsviews

我必须将一个xml视图控制器方法用于另一个XML View控制器。这是一个示例(仅限控制器文件):

**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>

第二视图:

**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>

我可以从加载的视图中获取数据,但是当我尝试重新加载view2时,loadedView1.getController()的值为 null

1 个答案:

答案 0 :(得分:1)

在组件中实例化全局JSONModel并在其中存储视图计数。通过在组件的 onInit 方法中调用 setModel ,可以在全球范围内使用该模型。