angularJS中批量提交请求的batch.js(2)
2015-09-18 09:58
841 查看
/// <reference path="./datajs-1.1.3.min.js" /> // This is a helper module that contains functions to make building and sending requests to our service easier. var ODataEFBatchSample = (function (odata, $) { return { // Posts a batch request to the service endpoint. postBatch: function (requests) { // Create a "TaskCompletionSource" to wrap the result of the callbacks to datajs into a promise interface. var deferred = $.Deferred(); // Fulfills the promise when the call to datajs is successful. var success = function (data, response) { deferred.resolve(data); }; // Rejects the promise when the call to datajs throws an error. var error = function (error, response) { deferred.reject(error); }; // Creates a batch request with the requests passed as argument. var batchRequest = { requestUri: "/odata/$batch", method: "POST", data: requests }; // Sends the request to the service asynchonously. odata.request(batchRequest, success, error, odata.batchHandler); // Returns a promise that will be fulfilled or rejected when the call to the service returns. return deferred.promise(); }, // Creates a batch builder to make building batch requests easier. createBatchBuilder: function () { var data = { __batchRequests: [] }; builder = {}; // Adds a changeset to the batch request and returns a changeSetBuilder in order to add requests // to the changeset. builder.addChangeSet = function () { var changeset = { __changeRequests: [] }; data.__batchRequests.push(changeset); var changeSetBuilder = {}; // Adds a request at the end of the current changeset and returns the changeSetBuilder to allow adding // more requests to the changeSet. changeSetBuilder.addRequest = function (request) { var requests = changeset.__changeRequests; requests.push(request); return changeSetBuilder; }; return changeSetBuilder; }; // Returns a batch request containing the changesets added using the builder. builder.getBatchRequest = function () { return data; }; return builder; }, // Queries the OData service for orders whose CustomerId is equal to the one indicated by key. getOrderLines: function (key) { // Create a "TaskCompletionSource" to wrap the result of the callbacks to datajs into a promise interface. var deferred = $.Deferred(); // Fulfills the promise when the call to datajs is successful. var success = function (data) { deferred.resolve(data); }; // Rejects the promise when the call to datajs throws an error. var error = function (error) { deferred.reject(error); }; // Sends the request to the service asynchonously. OData.read('/odata/Orders?$filter=CustomerId eq ' + key, success, error); // Returns a promise that will be fulfilled or rejected when the call to the service returns. return deferred.promise(); } }; })(OData, jQuery); var getResponses = function (data) { var batchResponses = data.__batchResponses; var changeSet = batchResponses[0]; return changeSet.__changeResponses; };
postBatch:提交批量请求
createBatchBuilder:收集请求,组成批量请求集合
getResponses:收到请求时返回的响应
相关文章推荐
- Using Angular 1.x With ES6 and Webpack
- 约战Angular中Promise(1)
- AngularJS进阶(六)AngularJS+BootStrap实现弹出对话框
- AngularJS进阶(六)AngularJS+BootStrap实现弹出对话框
- angular之service、factory预provider区别
- angular 依赖注入
- angular编辑页面跳转的问题
- Angular应用技巧
- 避免angular出现{{}}
- AngularJS的学习--$on、$emit和$broadcast的使用
- angularJS中批量提交请求的batch.js(1)
- 与AngularJS的约会之事件循环+watchers源码分析
- 学习笔记-AngularJs(三)
- angularJs编写多指令的情况
- ANGULARJS中使用JQUERY分页控件
- angularJS URL中的锚点(#)
- AngularJS使用 ng-options 实现传值给后台controller
- AngularJS使用 ng-repeat 中的 $index与替代方法
- 【ionic&AngularJS】用户头像压缩上传,按比例缩小。
- [Angular 2] 9. Replace ng-modle with #ref & events