Browse Source

lu request hooks

tags/v0.1.1
Robin Thoni 8 years ago
parent
commit
ca121c4b71
2 changed files with 42 additions and 9 deletions
  1. 1
    1
      dist/luticate-utils.min.js
  2. 41
    8
      src/DataAccess/lu-request.js

+ 1
- 1
dist/luticate-utils.min.js View File

@@ -1 +1 @@
1
-!function(){angular.module("luticate2Utils",[])}(),function(){"use strict";angular.module("luticate2Utils").factory("luBusiness",[function(){var a={};return a.create=function(){var a={};return a},a}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luBusyBusiness",["$q",function(a){var b={};return b.groups=null,b.initGroups=function(c){"string"==typeof c&&(c=[c]);for(var d=0;d<c.length;++d){var e=b.groups[c[d]];null==e&&(e={count:0,deferred:null,errors:[]},b.groups[c[d]]=e),0==e.count&&(e.deferred=a.defer(),e.errors=[]),++e.count}},b.isRunning=function(a){var c=b.groups[a];return null!=c?c.count>0:!1},b.promise=function(a){var c=b.groups[a];return null!=c&&null!=c.deferred?c.deferred.promise:null},b.hasError=function(a){var c=b.groups[a];return null!=c?0!=c.errors.length:null},b.errors=function(a){var c=b.groups[a];return null!=c?c.errors:null},b.resolve=function(a){"string"==typeof a&&(a=[a]);for(var c=0;c<a.length;++c){var d=b.groups[a[c]];--d.count,0==d.count&&(b.hasError(d)?d.deferred.resolve(null):d.deferred.resolve(null),d.deferred=null)}},b.reject=function(a,c){"string"==typeof a&&(a=[a]);for(var d=0;d<a.length;++d){var e=b.groups[a[d]];--e.count,e.errors.push(c),0==e.count&&(e.deferred.resolve(null),e.deferred=null)}},b.reset=function(){return b.groups={},b},b}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luWebApiCrudBusiness",["luBusiness","luWebApiDataAccess",function(a,b){var c={};return c.create=function(b){var c=a.create();return c.dataAccess=b,c.getSingleById=function(a,b){return c.dataAccess.getSingleById(a,b)},c.getMultiple=function(a,b,d,e,f){return c.dataAccess.getMultiple(a,b,d,e,f)},c.addDbo=function(a,b){return c.dataAccess.addDbo(a,b)},c.editSingleByIdDbo=function(a,b,d){return c.dataAccess.editSingleByIdDbo(a,b,d)},c.deleteDbo=function(a,b){return c.dataAccess.deleteDbo(a,b)},c},c}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luDataAccess",[function(){var a={};return a.create=function(){var a={};return a},a}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luRequest",["$q","$http","luBusyBusiness",function(a,b,c){function d(a,b,d,e){var f={data:b,status:d};a.reject(f),null!=e&&c.reject(e,f)}function e(a,b,d){a.resolve(b),null!=d&&c.resolve(d)}var f={};return f.get=function(a,b,c){return f.request(a,"GET",b,null,c)},f.post=function(a,b,c,d){return f.request(a,"POST",b,c,d)},f.put=function(a,b,c,d){return f.request(a,"PUT",b,c,d)},f["delete"]=function(a,b,c,d){return f.request(a,"DELETE",b,c,d)},f.request=function(f,g,h,i,j){null!=j&&c.initGroups(j);var k=a.defer();f=f.replace(/\/+$/,"");var l={url:f,method:g,params:h,data:JSON.stringify(i),headers:{"Content-Type":"application/json"}};return b(l).then(function(a){var b=a.data,c=a.status;null==b||"object"!=typeof b?d(k,{message:"Failed to parse response",code:c},c,j):null!=b.message?d(k,b,c,j):e(k,b.data,j)},function(a){var b=a.data,c=a.status;null==b||"object"!=typeof b?d(k,{message:"Failed to parse response",code:c},c,j):d(k,b,c,j)}),k.promise},f}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luWebApiCrudDataAccess",["luWebApiDataAccess",function(a){var b={};return b.create=function(b){var c=a.create(b);return c.getSingleById=function(a,b){return c.get(a,null,b)},c.getMultiple=function(a,b,d,e,f){return c.get("",{orderBy:a,filter:b,page:d,perPage:e},f)},c.addDbo=function(a,b){return c.post("",null,a,b)},c.editSingleByIdDbo=function(a,b,d){return c.post(a,null,b,d)},c.deleteDbo=function(a,b){return c["delete"](a,null,null,b)},c},b}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luWebApiDataAccess",["luDataAccess","luRequest",function(a,b){var c={};return c.create=function(c){var d=a.create();return d.entry_point=c,"/"!==d.entry_point.substr(-1)&&(d.entry_point+="/"),d.get=function(a,c,e){return b.get(d.entry_point+a,c,e)},d.post=function(a,c,e,f){return b.post(d.entry_point+a,c,e,f)},d.put=function(a,c,e,f){return b.put(d.entry_point+a,c,e,f)},d["delete"]=function(a,c,e,f){return b["delete"](d.entry_point+a,c,e,f)},d},c}])}();
1
+!function(){angular.module("luticate2Utils",[])}(),function(){"use strict";angular.module("luticate2Utils").factory("luBusiness",[function(){var a={};return a.create=function(){var a={};return a},a}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luBusyBusiness",["$q",function(a){var b={};return b.groups=null,b.initGroups=function(c){"string"==typeof c&&(c=[c]);for(var d=0;d<c.length;++d){var e=b.groups[c[d]];null==e&&(e={count:0,deferred:null,errors:[]},b.groups[c[d]]=e),0==e.count&&(e.deferred=a.defer(),e.errors=[]),++e.count}},b.isRunning=function(a){var c=b.groups[a];return null!=c?c.count>0:!1},b.promise=function(a){var c=b.groups[a];return null!=c&&null!=c.deferred?c.deferred.promise:null},b.hasError=function(a){var c=b.groups[a];return null!=c?0!=c.errors.length:null},b.errors=function(a){var c=b.groups[a];return null!=c?c.errors:null},b.resolve=function(a){"string"==typeof a&&(a=[a]);for(var c=0;c<a.length;++c){var d=b.groups[a[c]];--d.count,0==d.count&&(b.hasError(d)?d.deferred.resolve(null):d.deferred.resolve(null),d.deferred=null)}},b.reject=function(a,c){"string"==typeof a&&(a=[a]);for(var d=0;d<a.length;++d){var e=b.groups[a[d]];--e.count,e.errors.push(c),0==e.count&&(e.deferred.resolve(null),e.deferred=null)}},b.reset=function(){return b.groups={},b},b}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luWebApiCrudBusiness",["luBusiness","luWebApiDataAccess",function(a,b){var c={};return c.create=function(b){var c=a.create();return c.dataAccess=b,c.getSingleById=function(a,b){return c.dataAccess.getSingleById(a,b)},c.getMultiple=function(a,b,d,e,f){return c.dataAccess.getMultiple(a,b,d,e,f)},c.addDbo=function(a,b){return c.dataAccess.addDbo(a,b)},c.editSingleByIdDbo=function(a,b,d){return c.dataAccess.editSingleByIdDbo(a,b,d)},c.deleteDbo=function(a,b){return c.dataAccess.deleteDbo(a,b)},c},c}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luDataAccess",[function(){var a={};return a.create=function(){var a={};return a},a}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luRequest",["$q","$http","luBusyBusiness",function(a,b,c){function d(a,b,d,e,g,h,i,j){for(var k={data:i,status:j},l=0;l<f.hooks.reject.length;++l)f.hooks.reject[l](a,b,d,e,g,k);h.reject(k),null!=g&&c.reject(g,k)}function e(a,b,d,e,g,h,i){for(var j=0;j<f.hooks.resolve.length;++j)f.hooks.resolve[j](a,b,d,e,g,i);h.resolve(i.data),null!=g&&c.resolve(g)}var f={};return f.hooks={before:[],resolve:[],reject:[]},f.addHook=function(a,b){f.hooks.hasOwnProperty(a)&&f.hooks[a].push(b)},f.get=function(a,b,c){return f.request(a,"GET",b,null,c)},f.post=function(a,b,c,d){return f.request(a,"POST",b,c,d)},f.put=function(a,b,c,d){return f.request(a,"PUT",b,c,d)},f["delete"]=function(a,b,c,d){return f.request(a,"DELETE",b,c,d)},f.request=function(g,h,i,j,k){null!=k&&c.initGroups(k);var l=a.defer();g=g.replace(/\/+$/,"");for(var m={url:g,method:h,params:i,data:JSON.stringify(j),headers:{"Content-Type":"application/json"}},n=0;n<f.hooks.before.length;++n)f.hooks.before[n](g,h,i,j,k);return b(m).then(function(a){var b=a.data,c=a.status;null==b||"object"!=typeof b?d(g,h,i,j,k,l,{message:"Failed to parse response",code:c,version:null,data:null},c):null!=b.message?d(g,h,i,j,k,l,b,c):e(g,h,i,j,k,l,b)},function(a){var b=a.data,c=a.status;null==b||"object"!=typeof b?d(g,h,i,j,k,l,{message:"Failed to parse response",code:c,version:null,data:null},c):d(g,h,i,j,k,l,b,c)}),l.promise},f}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luWebApiCrudDataAccess",["luWebApiDataAccess",function(a){var b={};return b.create=function(b){var c=a.create(b);return c.getSingleById=function(a,b){return c.get(a,null,b)},c.getMultiple=function(a,b,d,e,f){return c.get("",{orderBy:a,filter:b,page:d,perPage:e},f)},c.addDbo=function(a,b){return c.post("",null,a,b)},c.editSingleByIdDbo=function(a,b,d){return c.post(a,null,b,d)},c.deleteDbo=function(a,b){return c["delete"](a,null,null,b)},c},b}])}(),function(){"use strict";angular.module("luticate2Utils").factory("luWebApiDataAccess",["luDataAccess","luRequest",function(a,b){var c={};return c.create=function(c){var d=a.create();return d.entry_point=c,"/"!==d.entry_point.substr(-1)&&(d.entry_point+="/"),d.get=function(a,c,e){return b.get(d.entry_point+a,c,e)},d.post=function(a,c,e,f){return b.post(d.entry_point+a,c,e,f)},d.put=function(a,c,e,f){return b.put(d.entry_point+a,c,e,f)},d["delete"]=function(a,c,e,f){return b["delete"](d.entry_point+a,c,e,f)},d},c}])}();

+ 41
- 8
src/DataAccess/lu-request.js View File

@@ -9,6 +9,19 @@
9 9
 
10 10
             var luRequest = {};
11 11
 
12
+            luRequest.hooks = {
13
+                before: [],
14
+                resolve: [],
15
+                reject: []
16
+            };
17
+
18
+            luRequest.addHook = function(type, callback)
19
+            {
20
+                if (luRequest.hooks.hasOwnProperty(type)) {
21
+                    luRequest.hooks[type].push(callback);
22
+                }
23
+            };
24
+
12 25
             luRequest.get = function(url, dataGet, luBusyGroups)
13 26
             {
14 27
                 return luRequest.request(url, 'GET', dataGet, null, luBusyGroups);
@@ -29,21 +42,27 @@
29 42
                 return luRequest.request(url, 'DELETE', dataGet, dataPost, luBusyGroups);
30 43
             };
31 44
 
32
-            function reject(deferred, data, status, luBusyGroups)
45
+            function reject(url, method, dataGet, dataPost, luBusyGroups, deferred, data, status)
33 46
             {
34 47
                 var error = {
35 48
                     data: data,
36 49
                     status: status
37 50
                 };
51
+                for (var i = 0; i < luRequest.hooks.reject.length; ++i) {
52
+                    luRequest.hooks.reject[i](url, method, dataGet, dataPost, luBusyGroups, error);
53
+                }
38 54
                 deferred.reject(error);
39 55
                 if (luBusyGroups != null) {
40 56
                     luBusyBusiness.reject(luBusyGroups, error);
41 57
                 }
42 58
             }
43 59
 
44
-            function resolve(deferred, data, luBusyGroups)
60
+            function resolve(url, method, dataGet, dataPost, luBusyGroups, deferred, data)
45 61
             {
46
-                deferred.resolve(data);
62
+                for (var i = 0; i < luRequest.hooks.resolve.length; ++i) {
63
+                    luRequest.hooks.resolve[i](url, method, dataGet, dataPost, luBusyGroups, data);
64
+                }
65
+                deferred.resolve(data.data);
47 66
                 if (luBusyGroups != null) {
48 67
                     luBusyBusiness.resolve(luBusyGroups);
49 68
                 }
@@ -69,27 +88,41 @@
69 88
                     }
70 89
                 };
71 90
 
91
+                for (var i = 0; i < luRequest.hooks.before.length; ++i) {
92
+                    luRequest.hooks.before[i](url, method, dataGet, dataPost, luBusyGroups);
93
+                }
94
+
72 95
                 $http(params)
73 96
                     .then(function (response) {
74 97
                         var result = response.data;
75 98
                         var status = response.status;
76 99
                         if (result == null || typeof result !== 'object') {
77
-                            reject(deferred, {message: "Failed to parse response", code: status}, status, luBusyGroups);
100
+                            reject(url, method, dataGet, dataPost, luBusyGroups, deferred, {
101
+                                message: "Failed to parse response",
102
+                                code: status,
103
+                                version: null,
104
+                                data: null
105
+                            }, status);
78 106
                         }
79 107
                         else if (result.message != null) {
80
-                            reject(deferred, result, status, luBusyGroups);
108
+                            reject(url, method, dataGet, dataPost, luBusyGroups, deferred, result, status);
81 109
                         }
82 110
                         else {
83
-                            resolve(deferred, result.data, luBusyGroups);
111
+                            resolve(url, method, dataGet, dataPost, luBusyGroups, deferred, result);
84 112
                         }
85 113
                     }, function (response) {
86 114
                         var result = response.data;
87 115
                         var status = response.status;
88 116
                         if (result == null || typeof result !== 'object') {
89
-                            reject(deferred, {message: "Failed to parse response", code: status}, status, luBusyGroups);
117
+                            reject(url, method, dataGet, dataPost, luBusyGroups, deferred, {
118
+                                message: "Failed to parse response",
119
+                                code: status,
120
+                                version: null,
121
+                                data: null
122
+                            }, status);
90 123
                         }
91 124
                         else {
92
-                            reject(deferred, result, status, luBusyGroups);
125
+                            reject(url, method, dataGet, dataPost, luBusyGroups, deferred, result, status);
93 126
                         }
94 127
                     });
95 128
                 return deferred.promise;

Loading…
Cancel
Save