简体   繁体   English

setJSON,setData和loadData有什么区别?

[英]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的基本功能:

setJSON setJSON

"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) { // ... } }; 

Source 资源

setData 使用setData

"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 }; 

Source 资源

loadData loadData

"Load data from the given remote URL and call setData " --> Please check the source here . “从给定的远程URL加载数据并调用setData ”->请在此处检查


In short, they all call setData at some point. 简而言之,它们都在某个时刻调用setData

Which API to call in which situation depends on in which format you have the data available. 在哪种情况下调用哪种API取决于可用数据的格式。

  • The data are in JSON text --> setJSON 数据以JSON文本-> setJSON
  • The data are somewhere else --> loadData 数据在其他地方-> loadData
  • I already have the data in JS object / array ---> setData 我已经在JS对象/数组中得到了数据---> setData

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

相关问题 SAPUI5中的router和eventBus有什么区别 - What is the difference between router and eventBus in SAPUI5 SAPUI5中attachMatched()和attachPatternMatched()和/或attachRouteMatched()以及attachRoutePatternMatched()之间有什么区别? - What is the difference between attachMatched() and attachPatternMatched() and/or attachRouteMatched() and attachRoutePatternMatched() in SAPUI5? SAPUI5 和 Fiori 应用在技术上有什么区别? - What is the difference between SAPUI5 and Fiori apps technically? sap.ui.core.routing.Router.navTo()和sap.m.routing.Targets.display()有什么区别? - What is the difference between sap.ui.core.routing.Router.navTo() and sap.m.routing.Targets.display()? sap.m.Title和sap.ui.core.Title有什么区别 - What is the difference between sap.m.Title vs. sap.ui.core.Title new sap.ui.model.odata.ODataModel和read之间有什么区别? - What is the difference between new sap.ui.model.odata.ODataModel and read? 在 manifest.json 和 index.html 中嵌入 SAPUI5 库有什么区别? - What is the difference between embedding SAPUI5 libraries in the manifest.json and the index.html? 如何在 JSONModel 的 loadData 方法中传递“mHeaders”? - How to pass 'mHeaders' in loadData method of JSONModel? 格式化程序和工厂功能之间的区别 - Difference between Formatter and Factory Function 使用xsjs和xsodata之间的区别? - Difference between use of xsjs and xsodata?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM