新的sap.ui.model.odata.ODataModel和read之间有什么区别?

新的sap.ui.model.odata.ODataModel和read之间有什么区别?,odata,sapui5,Odata,Sapui5,我正在玩OData服务,我很困惑什么时候使用它 var oModel = new sap.ui.model.odata.ODataModel("proxy/http/services.odata.org/V3/(S(k42qhed3hw4zgjxfnhivnmes))/OData/OData.svc"); this.getView().setModel(oModel); var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl

我正在玩OData服务,我很困惑什么时候使用它

var oModel = new sap.ui.model.odata.ODataModel("proxy/http/services.odata.org/V3/(S(k42qhed3hw4zgjxfnhivnmes))/OData/OData.svc");
this.getView().setModel(oModel);
var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
var productsModel = new JSONModel();

oModel.read("/Products",
     null,
     null,
     false,
     function _OnSuccess(oData, response) {
         var data = { "ProductCollection" : oData.results };
         productsModel.setData(data);
     },
     function _OnError(error) {
         console.log(error);
     }
);
this.getView().setModel(productsModel);
vs

var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
var productsModel = new JSONModel();

oModel.read("/Products",
     null,
     null,
     false,
     function _OnSuccess(oData, response) {
         var data = { "ProductCollection" : oData.results };
         productsModel.setData(data);
     },
     function _OnError(error) {
         console.log(error);
     }
);
this.getView().setModel(productsModel);

我有两个使用这两种方法的工作示例,但如果我可以在第一个版本中实现相同的效果,我无法理解为什么要使用read方法。请解释或指导我阅读可以消除混淆的文档。

好的,让我们从模型开始:

var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
var productsModel = new JSONModel();

oModel.read("/Products",
     null,
     null,
     false,
     function _OnSuccess(oData, response) {
         var data = { "ProductCollection" : oData.results };
         productsModel.setData(data);
     },
     function _OnError(error) {
         console.log(error);
     }
);
this.getView().setModel(productsModel);
  • JSON模型:JSON模型是一种客户端模型,因此适用于在客户端完全可用的小型数据集。JSON模型支持双向绑定。注意:在筛选、搜索、刷新时不进行服务器端调用

  • var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
  • OData模型:OData模型是服务器端模型:数据集仅在服务器上可用,客户端只知道当前可见的行和字段。这也意味着无法在客户端上进行排序和筛选。为此,客户端必须向服务器发送请求。意味着搜索/筛选再次调用odata服务

  • var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    现在,让我们看看我们将使用这些模型的场景:

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    场景1:以列表/表格/显示形式向用户显示数据。数据操作仅限于搜索和筛选。在这里,我将直接使用oData模型来控制,因为只需要获取数据(方法1)(注意:单向绑定)。请记住,所有更改都需要调用服务器

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    场景2:我有一个应用程序,它有多个输入,用户可以编辑更改,并且一些字段是计算字段和必填字段。总之,许多用户更改都是临时的,用户可能不想保存这些更改。在这里,您还不想将这些临时更改发送到后端。您需要在发送数据之前操作、验证数据。在这里,我们将在从odata模型(您的方法2)读取数据之后使用JSON模型。将更改存储在本地JSON模型中,验证并操作它们,最后使用Odata创建/更新发送数据。请记住,所有更改都不需要调用服务器,因为数据存在于本地JSON模型中

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    如果这对您有帮助,请告诉我。:)

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    编辑:其他信息:

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    根据你的评论:

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    文档中说oModel.read“触发get请求,但新的sap.ui.model.odata.ODataModel(“proxy/http/services.odata.org‌​/V3/(S)k42qhed3hw4zg‌​jxfnhivnmes))/OData/‌​svc)`做同样的事情,那么为什么以及何时使用oModel.read呢

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    这就是你误解的地方。代码
    new sap.ui.model.odata.ODataModel(“proxy/http/services.odata.org‌​/V3/(S)k42qhed3hw4zg‌​jxfnhivnmes))/OData/‌​OData.svc“
    不会发送读取/获取请求。它调用odata服务并获取服务的元数据。一个服务可以有多个实体。 例如:服务:具有多个实体集,例如类别、客户、员工等。因此,当我声明:
    new sap.ui.model.odata.ODataModel(“http://services.odata.org/Northwind/Northwind.svc/“”
    它将获取服务的元数据(非实际数据)。只有在调用所需的实体集时,它才会获取数据。实体集指定为:

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
  • 调用read方法时(如您指定的
    '/Products'
  • 将实体集名称直接绑定到控件,如列表、表等(
    items='{/Products}'

  • 好的,让我们从模型开始:

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
  • JSON模型:JSON模型是一种客户端模型,因此适用于在客户端完全可用的小型数据集。JSON模型支持双向绑定。注意:在筛选、搜索、刷新时不进行服务器端调用

  • var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
  • OData模型:OData模型是服务器端模型:数据集仅在服务器上可用,客户端只知道当前可见的行和字段。这也意味着无法在客户端上进行排序和筛选。为此,客户端必须向服务器发送请求。意味着搜索/筛选再次调用odata服务

  • var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    现在,让我们看看我们将使用这些模型的场景:

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    场景1:以列表/表格/显示形式向用户显示数据。数据操作仅限于搜索和筛选。在这里,我将直接使用oData模型来控制,因为只需要获取数据(方法1)(注意:单向绑定)。请记住,所有更改都需要调用服务器

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    场景2:我有一个应用程序,它有多个输入,用户可以编辑更改,并且一些字段是计算字段和必填字段。总之,许多用户更改都是临时的,用户可能不想保存这些更改。在这里,您还不想将这些临时更改发送到后端。您需要在发送数据之前操作、验证数据。在这里,我们将在从odata模型(您的方法2)读取数据之后使用JSON模型。将更改存储在本地JSON模型中,验证并操作它们,最后使用Odata创建/更新发送数据。请记住,所有更改都不需要调用服务器,因为数据存在于本地JSON模型中

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    如果这对您有帮助,请告诉我。:)

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    编辑:其他信息:

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    根据你的评论:

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    文档中说oModel.read“触发get请求,但新的sap.ui.model.odata.ODataModel(“proxy/http/services.odata.org‌​/V3/(S)k42qhed3hw4zg‌​jxfnhivnmes))/OData/‌​svc)`做同样的事情,那么为什么以及何时使用oModel.read呢

    var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
    var productsModel = new JSONModel();
    
    oModel.read("/Products",
         null,
         null,
         false,
         function _OnSuccess(oData, response) {
             var data = { "ProductCollection" : oData.results };
             productsModel.setData(data);
         },
         function _OnError(error) {
             console.log(error);
         }
    );
    this.getView().setModel(productsModel);
    
    这就是你误解的地方。代码
    new sap.ui.model.odata.ODataModel(“proxy/http/services.odata.org‌​/V3/(S)k42qhed3hw4zg‌​jxfnhivnmes))/OData/‌​OData.svc“
    不会发送读取/获取请求。它调用odata服务并获取