SAPUI5 从 JSON-Model 获取单个属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26295013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SAPUI5 get single property from JSON-Model
提问by dotchuZ
I am currently trying to figure out how I can retrieve a single value from a sap.ui.model.json.JSONModel
我目前正试图弄清楚如何从 sap.ui.model.json.JSONModel 中检索单个值
in my main view:
在我的主要观点中:
var gConfigModel = new sap.ui.model.json.JSONModel();
var getConfigCallback = function(config) {
gConfigModel.setData(config);
};
oController.getConfiguration(getConfigCallback);
console.log(gConfigModel);
in my controller:
在我的控制器中:
getConfiguration : function(callback) {
var sServiceUrl = "/sap/opu/odata/sap/xxx/ConfigurationSet('Initialize')";
var config = {};
callback(config);
$.getJSON(sServiceUrl).done(function(data) {
config = data.d;
callback(config);
});
},
In my console.log statement I can see that the data was successfully passed from the backend and successfully set to the JSON model. My requirement is to store the value of attribute Editable in a single variable.
在我的 console.log 语句中,我可以看到数据已成功从后端传递并成功设置为 JSON 模型。我的要求是将属性 Editable 的值存储在单个变量中。


I already tried gConfigModel.getProperty('/'), didnt work. tried to access gConfigModel.oData was undefined .. How can I store it in a single value?
我已经尝试过 gConfigModel.getProperty('/'),没有用。试图访问 gConfigModel.oData 未定义 .. 如何将其存储在单个值中?
Solution Comment:If you catch data from a backend, you have to take care how long it takes. data can be available later then expected, in my case I added 1s timeout, afterwards I can access the property easily
解决方案评论:如果您从后端捕获数据,则必须注意它需要多长时间。数据可以在预期之后可用,在我的情况下,我添加了 1s 超时,之后我可以轻松访问该属性
setTimeout(function() {
console.log(gConfigModel.getProperty('/Editable'));
}, 1000);
回答by Qualiture
I wouldn't advise using the model's getData()method since it is deprecated.
我不建议使用模型的getData()方法,因为它已被弃用。
A much better solution is to use gConfigModel.getProperty("/Editable")(I'm using the root slash here since your property resides in the root of your model)
一个更好的解决方案是使用gConfigModel.getProperty("/Editable")(我在这里使用根斜杠,因为您的属性驻留在模型的根中)
In the same way, you can also set your data:
gConfigModel.setProperty("/Editable", <your new value>)instead
以同样的方式,你还可以设置你的数据:
gConfigModel.setProperty("/Editable", <your new value>)代替
回答by Haojie
<script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js" id="sap-ui-bootstrap" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-libs="sap.m"></script>
<script>
function getConfiguration(callback) {
var sServiceUrl = "/sap/opu/odata/sap/xxx/ConfigurationSet('Initialize')";
var config = {};
var data = {
"d": {
"_metadata": "",
"Backup01": "01",
"Editable": "True"
}
};
setTimeout((function() {
config = data;
callback(config);
})(), 2000);
};
var gConfigModel = new sap.ui.model.json.JSONModel();
var getConfigCallback = function(config) {
gConfigModel.setData(config);
alert(gConfigModel.getProperty("/d/Editable"));
};
getConfiguration(getConfigCallback);
</script>
回答by Aleks Mi
First of all, thanks for the effort to find solutions of our Problems! (at least, those regarding It stuff.. :) )
首先,感谢您努力寻找我们问题的解决方案!(至少,那些关于它的东西...... :) )
I've found a solution which I think is a little bit more save because the timeout is maybe somewhat arbitrary - it would depend on the machine or the amount of data that is to be fetched?
我找到了一个我认为更省钱的解决方案,因为超时可能有点随意——这取决于机器或要获取的数据量?
Therefore, I am using an attachRequestCompletedfunction:
因此,我正在使用一个attachRequestCompleted函数:
with sUrl_2="path-to-my-service";
var oModel_2 = new sap.ui.model.json.JSONModel(sUrl_2);
oModel_2.attachRequestCompleted(function(data) {
//now, i can access the data stored in the oModel_2, either by getProperty, or by DOM: oModel_2.oData.d.Vendor
gv_selLieferant = oModel_2.getProperty("/d/Vendor");
gv_selEinkOrg = oModel_2.getProperty("/d/PurchOrg");
gv_selEinKGru = oModel_2.getProperty("/d/PurGroup");
});

