[英]What is the difference between setJSON, setData and loadData?
This is regarding the mentioned methods of sap.ui.model.json.JSONModel
in SAPUI5: 这与
sap.ui.model.json.JSONModel
中提到的sap.ui.model.json.JSONModel
方法有关:
setJSON
setData
loadData
What is the difference between these 3 methods? 这三种方法有什么区别? When do we use these methods and can we use more than 1 of them for the same purpose?
我们什么时候使用这些方法,并且出于相同的目的,我们可以使用其中的一种以上吗?
setData 使用setData
You have a JavaScript object and want to use this data as your model
您有一个JavaScript对象,并希望将此数据用作模型
const oJSONData = {
data: {
id: 4,
first_name: "Eve",
last_name: "Holt",
avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
}
};
oJSONModel.setData(oData);
setJSON setJSON
You have a String that when parsed represents a JavaScript object and want to use this data as your model
您有一个字符串,该字符串在解析时表示一个JavaScript对象,并希望将此数据用作模型
const sJSONData = '{"data":{"id":4,"first_name":"Eve","last_name":"Holt","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"}}';
oJSONModel.setJSON(sJSONData);
loadData loadData
You want to access a remote API which returns data as JSON and want to use this data as your model
您要访问一个远程API,该API以JSON格式返回数据,并希望将此数据用作模型
const sURL = "https://reqres.in/api/users/4";
oJSONModel.loadData(sURL);
Have a look at the well documented API Reference for JSONModel
. 看看有
JSONModel
API参考 。
In summary (from SAP Documentation): 总结(来自SAP文档):
setData
: Sets the data, passed as a JS object tree, to the model. setData
:将作为JS对象树传递的数据设置到模型。
eg 例如
var data = { "ProductCollection": [{ "titleId": 0, "Name": "Olayinka O", "ProductId": "001", "chartValue": 75, "ProductPicUrl": "sap-icon://competitor" }] }; var oModel = new sap.ui.model.json.JSONModel(data); //OR var oModel = new sap.ui.model.json.JSONModel(); oModel.setData(data); /*setdata, could also be a odata url in json format*/
loadData
: Load JSON-encoded data from the server using a GET HTTP request and store the resulting JSON data in the model. loadData
:使用GET HTTP请求从服务器加载JSON编码的数据,并将结果JSON数据存储在模型中。 Note: Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy, the request can not successfully retrieve data from a different domain, subdomain, or protocol. 注意:由于浏览器安全限制,大多数“ Ajax”请求都受相同的原始策略约束,因此该请求无法从其他域,子域或协议成功检索数据。
eg you can use this to load/GET changes to the data/model and automatically updates the view if that specific model has being binded by reloading the url. 例如,您可以使用它来加载/获取对数据/模型的更改,如果通过重新加载URL绑定了特定模型,则可以自动更新视图。 If you use load , you don't need the other two in my opinion and
loadData
with not work on local json data. 如果您使用load ,则我认为不需要其他两个,并且
loadData
不适用于本地json数据。
var sURL = "https://cors-anywhere.herokuapp.com/https://services.odata.org/V3/Northwind/Northwind.svc/Products?$format=json"; var oModel = new sap.ui.model.json.JSONModel(); //if called in setInterval, all changes in the backend will be updated in the view if binded in this case every second setInterval(oModel.loadData(sURL, true), 1000);
setJSON
: Sets the data, passed as a string in JSON format, to the model. setJSON
:将数据(以JSON格式的字符串形式传递)设置到模型。
ie Same as Set Data but strict JSON 即与设置数据相同,但使用严格的JSON
Luckily, the source code of UI5 is quite readable and often the better documentation than most of the API descriptions. 幸运的是,与大多数API描述相比,UI5的源代码可读性强,并且通常是更好的文档。 Here is what each one of the APIs does basically:
以下是每个API的基本功能:
"Parse the JSON text and call setData
" “解析JSON文本并调用
setData
”
JSONModel.prototype.setJSON = function(sJSON, bMerge) { var oJSONData; try { oJSONData = jQuery.parseJSON(sJSON); this.setData(oJSONData, bMerge); } catch (e) { // ... } };
"Store the data and notify all dependent bindings ( checkUpdate
)" “存储数据并通知所有依赖的绑定(
checkUpdate
)”
JSONModel.prototype.setData = function(oData/*plain JS object*/, bMerge){ if (bMerge) { this.oData = /* merge with existing data */; } else { this.oData = oData; } // ... this.checkUpdate(); // notifies dependent bindings };
"Load data from the given remote URL and call setData
" --> Please check the source here . “从给定的远程URL加载数据并调用
setData
”->请在此处检查源 。
setData
at some point. setData
。 Which API to call in which situation depends on in which format you have the data available. 在哪种情况下调用哪种API取决于可用数据的格式。
setJSON
setJSON
loadData
loadData
setData
setData
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.