You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequestDataAccess.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (function () {
  2. 'use strict';
  3. angular.module('appSdk')
  4. .factory('RequestDataAccess', ['$q', '$http', 'PromisesDataAccess', function ($q, $http, PromisesDataAccess) {
  5. var DataAccess = {};
  6. DataAccess.get = function(url, dataGet, promise)
  7. {
  8. return DataAccess.request(url, 'GET', dataGet, null, promise);
  9. };
  10. DataAccess.post = function(url, dataPost, dataGet, promise)
  11. {
  12. return DataAccess.request(url, 'POST', dataGet, dataPost, promise);
  13. };
  14. DataAccess.put = function(url, dataPost, dataGet, promise)
  15. {
  16. return DataAccess.request(url, 'PUT', dataGet, dataPost, promise);
  17. };
  18. DataAccess.delete = function(url, dataPost, dataGet, promise)
  19. {
  20. return DataAccess.request(url, 'DELETE', dataGet, dataPost, promise);
  21. };
  22. function reject(defered, data, status, promise)
  23. {
  24. var obj = {
  25. Data: data,
  26. Status: status
  27. };
  28. defered.reject(obj);
  29. if (promise != null) {
  30. PromisesDataAccess.reject(promise, obj);
  31. }
  32. }
  33. function resolve(defered, data, promise)
  34. {
  35. defered.resolve(data);
  36. if (promise != null) {
  37. PromisesDataAccess.resolve(promise, data);
  38. }
  39. }
  40. DataAccess.request = function(url, method, dataGet, dataPost, promise)
  41. {
  42. if (promise != null) {
  43. PromisesDataAccess.initPromise(promise);
  44. }
  45. var defered = $q.defer();
  46. url = url.replace(/\/+$/, '');
  47. var params = {
  48. url: url,
  49. method: method,
  50. params: dataGet,
  51. data: dataPost == null ? null : $.param(dataPost),
  52. headers: {
  53. 'Content-Type': "application/x-www-form-urlencoded"
  54. }
  55. };
  56. $http(params)
  57. .success(function (result, status) {
  58. resolve(defered, result, promise);
  59. })
  60. .error(function (result, status) {
  61. reject(defered, result, status, promise);
  62. });
  63. return defered.promise;
  64. };
  65. return DataAccess;
  66. }]);
  67. })();