来自另一个控制器的SAPUI5 Access json模型 [英] SAPUI5 Access json model from another controller

查看:96
本文介绍了来自另一个控制器的SAPUI5 Access json模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SAPUI5的新手,我正在做一些有关母版"和细节"页面的练习.

i am new to SAPUI5 and im doing some exercises about Master and Detail pages.

我有2个视图,分别是 Master.view.xml Detail.view.xml . Master.view.xml 包含员工列表.当用户单击员工时,它将导航到详细信息视图. Detail.view.xml 包含所选员工的详细信息.

I have 2 views, Master.view.xml and Detail.view.xml. The Master.view.xml contains a list of employees. When the user clicks on an employee, it will navigate to the detail view. The Detail.view.xml contains the detail of the selected employee.

每个视图都有其对应的控制器. 我已将json模型放入 Master.controller.js 中.

Each view has its corresponding controller. I've put my json model in the Master.controller.js.

如何在不传递数据的情况下从 Master.controller.js 访问 Detail.view.xml 的json模型?这有可能吗?

How can i access the json model from the Master.controller.js to the Detail.view.xml without passing the data? Is this even possible?

@keshet

这是我在母版"页面中创建的列表:

This is the List i have created in the Master page:

<List  xmlns="sap.m"
            id="masterList"
                headerText="{i18n>masterHeaderText}"
                footerText="Updated: September 4, 2015"
                items="{/employeeDetails}"
                itemPress="" >
                    <StandardListItem xmlns="sap.m" 
                        id="{empid}"
                        title="{name}"
                        description="{levelDesc}"
                        info="{cubeNo}"
                        type="Navigation"
                        tap="handleListItemPress"  />
            </List>

现在,我正在尝试将StandardListItem的ID更改为来自模型的员工的ID.但是出现一个错误,说我不能使用 empid 作为StandardListItem的ID.我该怎么做?

Now i am trying to change the id of the StandardListItem to the ID of the employee that is from the model. But an error appears saying i cant use the empid as id of the StandardListItem. How do i do this?

推荐答案

您可以全局声明模型,因此所有视图都可以访问它. 定义全局模型:

You can declare your model globally, so all views will have access to it. To define global model:

var oModel = new sap.ui.model.json.JSONModel(oData);
sap.ui.getCore().setModel(oModel);

要在另一个视图中从模型中检索数据,请执行以下操作:

To retrieve the data from the model in another view:

var oData = sap.ui.getCore().getModel().getData();

如果要使用多个全局模型,还可以为模型命名:

You also can give a name to your model if you want to use several global models:

var oModel = new sap.ui.model.json.JSONModel(oData);
sap.ui.getCore().setModel(oModel, "modelname");

var oData = sap.ui.getCore().getModel("modelname").getData();

这篇关于来自另一个控制器的SAPUI5 Access json模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆