简体   繁体   English

如何从 get odata 调用方法中获取变量

[英]How to get variables out of a get odata call method

What is the best way to get variables out of a get odata call.从 get odata 调用中获取变量的最佳方法是什么。 I have this code in a method where i want to work with the variable 'id', which i am getting with the odata call.我在一个方法中有这段代码,我想在其中使用变量“id”,这是我通过 odata 调用获得的。 I tried already callbacks but i cant get the right way.我已经尝试过回调,但我无法找到正确的方法。 Do you have a solution for this?你有解决办法吗? I tried also to put the call in an extra function, but then i get the problem that by return variable is undefined ia cant access it.我还尝试将调用放在一个额外的函数中,但是后来我遇到了一个问题,即返回变量未定义,我无法访问它。

Update / additional:更新/补充:

I have a method where i am doing some odata updates and inserts.我有一种方法可以进行一些 odata 更新和插入。 in my code below i am getting a id.在我下面的代码中,我得到了一个 ID。 this id i need in the next step for another odata update as parameter.在下一步中我需要这个 id 作为另一个 odata 更新作为参数。 My problem is that i cant work with the var id in the next steps of my code, because its only accessable in the oModel {} section.我的问题是我无法在代码的下一步中使用 var id,因为它只能在 oModel {} 部分访问。

oModel.read("/ZDEMA_LENDINGS2Set", {
    urlParameters: {
        "$select": "Id",
        "$top": 1
    },
    success: function(oData, oResponse) {
        console.log("Data", oData);
        console.log("Response", oResponse); 
        var id = oData.results[0].Id;
    },
    error: function(oError) {
        console.log("Error", oError);
    }
}

Since this an UI5 app, I suggest using a local JSON Model and set the value as property.由于这是一个 UI5 应用程序,我建议使用本地 JSON 模型并将值设置为属性。

this.getView().getModel('myLocalModel').setProperty("lendingId", oData.results[0].Id)

This has the advantage that you can use the two way databinding and or change listeners.这样做的好处是您可以使用两种方式数据绑定和/或更改侦听器。 Alternative approach is to fire an event on the event bus.另一种方法是在事件总线上触发一个事件。

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

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