Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PromisesDataAccess.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Created by robin on 10/24/15.
  3. */
  4. (function () {
  5. 'use strict';
  6. angular.module('appSdk')
  7. .factory('PromisesDataAccess', [function() {
  8. var luticatePromises = {};
  9. luticatePromises.promises = {};
  10. luticatePromises.loaders = [];
  11. luticatePromises.errors = [];
  12. luticatePromises.build = function(array)
  13. {
  14. luticatePromises[array] = {};
  15. for (var id in luticatePromises.promises) {
  16. var promise = luticatePromises.promises[id];
  17. promise[array].forEach(function(group) {
  18. if (luticatePromises[array][group] == null) {
  19. luticatePromises[array][group] = [];
  20. }
  21. luticatePromises[array][group].push(promise);
  22. });
  23. }
  24. };
  25. luticatePromises.initPromise = function(data) {
  26. var p = {
  27. loaders : [],
  28. errors: [],
  29. status: 0,
  30. id: data.id
  31. };
  32. if (data.groups) {
  33. data.groups.forEach(function (group){
  34. p.loaders.push(group);
  35. p.errors.push(group);
  36. });
  37. }
  38. if (data.loaderGroups) {
  39. data.loaderGroups.forEach(function(group)
  40. {
  41. p.loaders.push(group);
  42. });
  43. }
  44. if (data.errorGroups) {
  45. data.errorGroups.forEach(function(group)
  46. {
  47. p.errors.push(group);
  48. });
  49. }
  50. luticatePromises.promises[data.id] = p;
  51. luticatePromises.build("loaders");
  52. luticatePromises.build("errors");
  53. };
  54. luticatePromises.getLoadersGroup = function(group) {
  55. if (luticatePromises.loaders[group] == null) {
  56. return null;
  57. }
  58. return luticatePromises.loaders[group];
  59. };
  60. luticatePromises.getErrorsGroup = function(group) {
  61. if (luticatePromises.errors[group] == null) {
  62. return null;
  63. }
  64. return luticatePromises.errors[group];
  65. };
  66. luticatePromises.reject = function(promise, error) {
  67. luticatePromises.promises[promise.id].status = 2;
  68. luticatePromises.promises[promise.id].value = error;
  69. };
  70. luticatePromises.resolve = function(promise, data) {
  71. luticatePromises.promises[promise.id].status = 1;
  72. luticatePromises.promises[promise.id].value = data;
  73. };
  74. return luticatePromises;
  75. }]);
  76. })();