简体   繁体   English

oData 模型的 SAP UI5 访问值

[英]SAP UI5 access value of oData model

I'm getting started with SAPUI5 these weeks and built my first app that displays some data.这几周我开始使用 SAPUI5,并构建了我的第一个显示一些数据的应用程序。 I even managed to do some filtering but now I encounter an error that I do not understand.我什至设法进行了一些过滤,但现在我遇到了一个我不明白的错误。

My oData Model has the following structure:我的 oData 模型具有以下结构:

Notice('0000'):
  Value0: abc
  Value1: def
  Value2: ghi


Notice('0001'):
  Value0: abc
  Value1: def
  Value2: ghi

I have a table holding the data:我有一个保存数据的表:

<Table
    id="noticeList"
    class="sapUiResponsiveMargin"
    width="auto"
    items="{
        path : '/Notice',
        sorter : {
            path : 'Value0'
        }
    }">

...some header stuff and column delaration... ...一些标题的东西和列delaration...

      <items>
        <ColumnListItem
             id="noticeColumnListItem"
             items="{
                path : '/Notice'
            }">
            <cells>
                <ObjectStatus text="{Value0}"/>
                <ObjectStatus text="{Value1}"/>
                <ObjectStatus text="{Value2}"/>
           </cells>
        </ColumnListItem>
    </items>
</Table>

Now, I want to access the Data of my Model (no matter if before or after rendering).现在,我想访问我的模型的数据(无论是在渲染之前还是之后)。 I do not have a event to trigger this, I just need the information as soon as the model data was transferred.我没有触发这个的事件,我只需要模型数据传输后的信息。

So I searched a little and found the following:所以我搜索了一下,发现了以下内容:

var myValue = this.byId("noticeList").getModel().getProperty("/Notice/Value0");

I tried every possible path but always end up in that myValue is undefined.我尝试了所有可能的路径,但总是以 myValue 未定义而告终。 When I dump my model, I can see that it holds an object oData with the needed values.当我转储我的模型时,我可以看到它包含一个具有所需值的对象 oData。 Also, my list is dispayed correct but I just don't manage to read a single value from the Model.此外,我的列表显示正确,但我无法从模型中读取单个值。

This is how I instanciate it in Component.js:这是我在 Component.js 中实例化它的方式:

config: {
    fullWidth: true,
    resourceBundle: "i18n/messageBundle.properties",
    serviceConfig: {
        name: "MYJOBS",
        serviceUrl: "/sap/opu/odata/sap/PATH_TO_SERVICE"
    }
},

// Read service URL
var sServiceUrl = mConfig.serviceConfig.serviceUrl;

// Create and set domain model to the component

var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, {
    json: true,
    loadMetadataAsync: true
});

oModel.attachMetadataFailed(function() {
    sap.ui.getCore().getEventBus().publish("Component", "MetadataFailed");
}, this);

//var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl);
this.setModel(oModel);

Maybe I should also mention that I don't use index.html to load the app but do this via Fiori Launchpad and Component.js也许我还应该提到我不使用 index.html 来加载应用程序,而是通过 Fiori Launchpad 和 Component.js 来加载应用程序

I spent hours over hours on this problem and can't find a solution, I hope someone can help...我在这个问题上花了几个小时,但找不到解决方案,我希望有人能帮忙...

Kind regards!亲切的问候!

If you are calling a remote service the most common cause of an undefined scenario is the delay in returning the data with the other code continuing to execute (as you are calling your service asynchronously).如果您正在调用远程服务,则未定义场景的最常见原因是在其他代码继续执行时返回数据的延迟(因为您正在异步调用您的服务)。

If you are trying to act on the returned data then you could do this by attaching an event listener / anonyomous function in the following manner:如果您试图对返回的数据采取行动,那么您可以通过以下方式附加事件侦听器/匿名函数来执行此操作:

oModel.attachRequestCompleted(function(){
                    // do something
                });

In your scenario your controller code could be changed to look as follows:在您的场景中,您的控制器代码可以更改为如下所示:

 onInit: function() {

   var sServiceUrl = "/sap/opu/odata/sap/yourPathSRV/";
   var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl);
   oModel.attachRequestCompleted(function(){
                    // do something
                });
   this.getView().setModel(oModel);
}

Bernard's answer was the solving one!伯纳德的答案是解决问题的答案!

The problem was that I tried to access the data too early.问题是我试图过早地访问数据。 I'd alerady tried to avoid this by using a self-built sleep function but this failed (because sleeping just extended the request time).我已经尝试通过使用自建的睡眠功能来避免这种情况,但这失败了(因为睡眠只是延长了请求时间)。

This has done the trick:这已经成功了:

this.byId("noticeList").getModel().attachRequestCompleted(function(oEvent){

    var model = oEvent.getSource();

    var myNeededValue = model.getProperty("/NoticeSet('0001')/Value0"));

});

Thanks a lot!非常感谢!

First of all, I hope I get everything right und you get my Point at the end :-)首先,我希望我做对了一切,最后你得到了我的观点:-)

For the correct element structure of folders etc you should get the trial version of Web IDE and use the template "SAPUI5".对于文件夹等的正确元素结构,您应该获得 Web IDE 的试用版并使用模板“SAPUI5”。 Don't underestimate the structure :-)不要低估结构:-)

The structure of your app should be:你的应用程序的结构应该是:
Parent Folder父文件夹
webapp folder webapp文件夹
on the same level now: component.js / index.html and folders (eg for views and controller or i18n)现在处于同一级别:component.js / index.html 和文件夹(例如用于视图和控制器或 i18n)

1) Component.js / manifest.json (in newer versions > 1.32) 1) Component.js / manifest.json(在较新的版本中 > 1.32)
--> it contains only information about the model instanciation, i18n instance, service url, target and route pattern. --> 它只包含关于模型实例化、i18n 实例、服务 URL、目标和路由模式的信息。 No model loading in here!这里没有模型加载!

config: {
    fullWidth : true,
    resourceBundle: "i18n/messageBundle.properties",
    serviceConfig: {
        type: "OData",  // type is missing
        uri: "/sap/opu/odata/sap/PATH_TO_SERVICE" // use uri as property
    }

},

2) Controller (in onInit-hook-method) 2)控制器(在onInit-hook-method中)

 onInit: function() {

   var sServiceUrl = "/sap/opu/odata/sap/yourPathSRV/";
   var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl);
   this.getView().setModel(oModel);

}

3) View (content area) 3)查看(内容区)

<mvc:View controllerName="TEST.controller.Main"
   xmlns:l="sap.ui.layout"
   xmlns:mvc="sap.ui.core.mvc" 
   xmlns="sap.m">
 <App>
  <pages>
   <Page title="{i18n>title}">
    <content>
    <List id="yourListId" items="{/Notice}">
      <StandardListItem description="{Value0}" title="{Value1}"/>
     </List>
    </content>
   </Page>
  </pages>
 </App>
</mvc:View>

4) Backend 4) 后端

--> You need to implement GET_ENTITYSET - method to read "multiple" notice-entities :-) --> 您需要实现 GET_ENTITYSET - 读取“多个”通知实体的方法:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM