Browse Source

added luDataInitializer

tags/v0.3.1
Robin Thoni 7 years ago
parent
commit
2b5f3c09e6

+ 8
- 6
src/Business/lu-webapi-crud-business.js View File

@@ -14,34 +14,36 @@
14 14
 
15 15
                 Business.dataAccess = dataAccess;
16 16
 
17
+                Business.dboInitializer = Business.dataAccess.dboInitializer;
18
+
17 19
                 Business.initDbo = function(dbo)
18 20
                 {
19
-                    return Business.dataAccess.initDbo(dbo);
21
+                    return Business.dataAccess.dboInitializer.initDbo(dbo, Business.dataAccess.TYPE_DBO);
20 22
                 };
21 23
 
22 24
                 Business.initListDbo = function(list)
23 25
                 {
24
-                    return Business.dataAccess.initListDbo(list);
26
+                    return Business.dataAccess.dboInitializer.initListDbo(list, Business.dataAccess.TYPE_DBO);
25 27
                 };
26 28
 
27 29
                 Business.initPaginatedDbo = function(data)
28 30
                 {
29
-                    return Business.dataAccess.initPaginatedDbo(data);
31
+                    return Business.dataAccess.dboInitializer.initPaginatedDbo(data, Business.dataAccess.TYPE_DBO);
30 32
                 };
31 33
 
32 34
                 Business.thenInitDbo = function(promise)
33 35
                 {
34
-                    return Business.dataAccess.thenInitDbo(promise);
36
+                    return Business.dataAccess.dboInitializer.thenInitDbo(promise, Business.dataAccess.TYPE_DBO);
35 37
                 };
36 38
 
37 39
                 Business.thenInitListDbo = function(promise)
38 40
                 {
39
-                    return Business.dataAccess.thenInitListDbo(promise);
41
+                    return Business.dataAccess.dboInitializer.thenInitListDbo(promise, Business.dataAccess.TYPE_DBO);
40 42
                 };
41 43
 
42 44
                 Business.thenInitPaginatedDbo = function(promise)
43 45
                 {
44
-                    return Business.dataAccess.thenInitPaginatedDbo(promise);
46
+                    return Business.dataAccess.dboInitializer.thenInitPaginatedDbo(promise, Business.dataAccess.TYPE_DBO);
45 47
                 };
46 48
 
47 49
 

+ 72
- 0
src/DataAccess/lu-data-initializer.js View File

@@ -0,0 +1,72 @@
1
+/**
2
+ * Created by robin on 12/11/16.
3
+ */
4
+
5
+(function () {
6
+    'use strict';
7
+    angular.module('luticate2Utils')
8
+        .factory('luDataInitializer', ['$q',
9
+            function ($q) {
10
+
11
+            var luDataInitializer = {};
12
+
13
+            luDataInitializer.create = function (initData) {
14
+                var initializer = {};
15
+
16
+                initializer.initData = initData;
17
+
18
+                initializer.initListData = function(list, userData)
19
+                {
20
+                    var d = [];
21
+                    for (var i = 0; i < list.length; ++i) {
22
+                        d.push(initializer.initData(list[i], userData));
23
+                    }
24
+                    return d;
25
+                };
26
+
27
+                initializer.initPaginatedData = function(data, userData)
28
+                {
29
+                    var d = {
30
+                        count: data.count,
31
+                        data: initializer.initListData(data.data, userData)
32
+                    };
33
+                    return d;
34
+                };
35
+
36
+                initializer.thenInitData = function(promise, userData)
37
+                {
38
+                    var deferred = $q.defer();
39
+                    promise.then(function(data)
40
+                    {
41
+                        deferred.resolve(initializer.initData(data, userData));
42
+                    }, deferred.reject);
43
+                    return deferred.promise;
44
+                };
45
+
46
+                initializer.thenInitListData = function(promise, userData)
47
+                {
48
+                    var deferred = $q.defer();
49
+                    promise.then(function(data)
50
+                    {
51
+                        deferred.resolve(initializer.initListData(data, userData));
52
+                    }, deferred.reject);
53
+                    return deferred.promise;
54
+                };
55
+
56
+                initializer.thenInitPaginatedData = function(promise, userData)
57
+                {
58
+                    var deferred = $q.defer();
59
+                    promise.then(function(data)
60
+                    {
61
+                        deferred.resolve(initializer.initPaginatedData(data, userData));
62
+                    }, deferred.reject);
63
+                    return deferred.promise;
64
+                };
65
+
66
+                return initializer;
67
+            };
68
+
69
+            return luDataInitializer;
70
+
71
+        }]);
72
+})();

+ 45
- 128
src/DataAccess/lu-webapi-crud-dataaccess.js View File

@@ -5,8 +5,8 @@
5 5
 (function () {
6 6
     'use strict';
7 7
     angular.module('luticate2Utils')
8
-        .factory('luWebApiCrudDataAccess', ['luWebApiDataAccess', '$q', 'luUtilsDataAccess',
9
-            function (luWebApiDataAccess, $q, luUtilsDataAccess) {
8
+        .factory('luWebApiCrudDataAccess', ['luWebApiDataAccess', '$q', 'luUtilsDataAccess', 'luDataInitializer',
9
+            function (luWebApiDataAccess, $q, luUtilsDataAccess, luDataInitializer) {
10 10
 
11 11
             var luWebApiCrudDataAccess = {};
12 12
 
@@ -16,6 +16,31 @@
16 16
                 DataAccess.OP_EDIT = 'OP_EDIT';
17 17
                 DataAccess.OP_ADD = 'OP_ADD';
18 18
 
19
+                DataAccess.TYPE_DBO = 'TYPE_DBO';
20
+                DataAccess.TYPE_MODEL = 'TYPE_MODEL';
21
+
22
+                DataAccess.defaultDbo = {
23
+                    id: null,
24
+                    createdAt: null,
25
+                    updatedAt: null,
26
+                    toString: function()
27
+                    {
28
+                        return this.id;
29
+                    }
30
+                };
31
+
32
+
33
+                DataAccess.modelInitializer = luDataInitializer.create(function(model, op)
34
+                {
35
+                    return DataAccess.initModel(model, op);
36
+                });
37
+
38
+                DataAccess.dboInitializer = luDataInitializer.create(function(dbo, type)
39
+                {
40
+                    return DataAccess.initDbo(dbo, type);
41
+                });
42
+
43
+
19 44
                 DataAccess._initModel = function(model, op)
20 45
                 {
21 46
                     if (model == null) {
@@ -35,65 +60,21 @@
35 60
                     return DataAccess._initModel(model, op);
36 61
                 };
37 62
 
38
-                DataAccess.initListModel = function(list, op)
39
-                {
40
-                    var d = [];
41
-                    for (var i = 0; i < list.length; ++i) {
42
-                        d.push(DataAccess.initModel(list[i], op));
43
-                    }
44
-                    return d;
45
-                };
46 63
 
47
-                DataAccess.initPaginatedModel = function(data, op)
64
+                DataAccess._initDbo = function(model, type)
48 65
                 {
49
-                    var d = {
50
-                        count: data.count,
51
-                        data: DataAccess.initListModel(data.data, op)
52
-                    };
53
-                    return d;
54
-                };
55
-
56
-                DataAccess.thenInitModel = function(promise, op)
57
-                {
58
-                    var deferred = $q.defer();
59
-                    promise.then(function(data)
60
-                    {
61
-                        deferred.resolve(DataAccess.initModel(data, op));
62
-                    }, deferred.reject);
63
-                    return deferred.promise;
64
-                };
65
-
66
-                DataAccess.thenInitListModel = function(promise, op)
67
-                {
68
-                    var deferred = $q.defer();
69
-                    promise.then(function(data)
70
-                    {
71
-                        deferred.resolve(DataAccess.initListModel(data, op));
72
-                    }, deferred.reject);
73
-                    return deferred.promise;
66
+                    if (model == null) {
67
+                        return null;
68
+                    }
69
+                    var dbo = DataAccess.extendDeep({}, DataAccess.defaultDbo, model);
70
+                    dbo.createdAt = luUtilsDataAccess.stringToMomentDateTime(dbo.createdAt);
71
+                    dbo.updatedAt = luUtilsDataAccess.stringToMomentDateTime(dbo.updatedAt);
72
+                    return dbo;
74 73
                 };
75 74
 
76
-                DataAccess.thenInitPaginatedModel = function(promise, op)
75
+                DataAccess.initDbo = function(model, type)
77 76
                 {
78
-                    var deferred = $q.defer();
79
-                    promise.then(function(data)
80
-                    {
81
-                        deferred.resolve(DataAccess.initPaginatedModel(data, op));
82
-                    }, deferred.reject);
83
-                    return deferred.promise;
84
-                };
85
-
86
-
87
-
88
-
89
-                DataAccess.defaultDbo = {
90
-                    id: null,
91
-                    createdAt: null,
92
-                    updatedAt: null,
93
-                    toString: function()
94
-                    {
95
-                        return this.id;
96
-                    }
77
+                    return DataAccess._initDbo(model, type);
97 78
                 };
98 79
 
99 80
                 DataAccess.extendDeep = function extendDeep(dst) {
@@ -110,98 +91,34 @@
110 91
                     });
111 92
                     return dst;
112 93
                 };
113
-
114
-                DataAccess._initDbo = function(model)
115
-                {
116
-                    if (model == null) {
117
-                        return null;
118
-                    }
119
-                    var dbo = DataAccess.extendDeep({}, DataAccess.defaultDbo, model);
120
-                    dbo.createdAt = luUtilsDataAccess.stringToMomentDateTime(dbo.createdAt);
121
-                    dbo.updatedAt = luUtilsDataAccess.stringToMomentDateTime(dbo.updatedAt);
122
-                    return dbo;
123
-                };
124
-
125
-                DataAccess.initDbo = function(model)
126
-                {
127
-                    return DataAccess._initDbo(model);
128
-                };
129
-
130
-                DataAccess.initListDbo = function(list)
131
-                {
132
-                    var d = [];
133
-                    for (var i = 0; i < list.length; ++i) {
134
-                        d.push(DataAccess.initDbo(list[i]));
135
-                    }
136
-                    return d;
137
-                };
138
-
139
-                DataAccess.initPaginatedDbo = function(data)
140
-                {
141
-                    var d = {
142
-                        count: data.count,
143
-                        data: DataAccess.initListDbo(data.data)
144
-                    };
145
-                    return d;
146
-                };
147
-
148
-                DataAccess.thenInitDbo = function(promise)
149
-                {
150
-                    var deferred = $q.defer();
151
-                    promise.then(function(data)
152
-                    {
153
-                        deferred.resolve(DataAccess.initDbo(data));
154
-                    }, deferred.reject);
155
-                    return deferred.promise;
156
-                };
157
-
158
-                DataAccess.thenInitListDbo = function(promise)
159
-                {
160
-                    var deferred = $q.defer();
161
-                    promise.then(function(data)
162
-                    {
163
-                        deferred.resolve(DataAccess.initListDbo(data));
164
-                    }, deferred.reject);
165
-                    return deferred.promise;
166
-                };
167
-
168
-                DataAccess.thenInitPaginatedDbo = function(promise)
169
-                {
170
-                    var deferred = $q.defer();
171
-                    promise.then(function(data)
172
-                    {
173
-                        deferred.resolve(DataAccess.initPaginatedDbo(data));
174
-                    }, deferred.reject);
175
-                    return deferred.promise;
176
-                };
177 94
                 
178 95
                 
179 96
                 
180 97
                 DataAccess.getSingleById = function (id, luBusyGroups) {
181
-                    return DataAccess.thenInitDbo(DataAccess.get(id, null, luBusyGroups));
98
+                    return DataAccess.dboInitializer.thenInitData(DataAccess.get(id, null, luBusyGroups), DataAccess.TYPE_MODEL);
182 99
                 };
183 100
 
184 101
                 DataAccess.getMultiple = function (orderBy, filter, page, perPage, luBusyGroups) {
185
-                    return DataAccess.thenInitPaginatedDbo(DataAccess.get('', {
102
+                    return DataAccess.dboInitializer.thenInitPaginatedData(DataAccess.get('', {
186 103
                         orderBy: orderBy,
187 104
                         filter: filter,
188 105
                         page: page,
189 106
                         perPage: perPage
190
-                    }, luBusyGroups));
107
+                    }, luBusyGroups), DataAccess.TYPE_MODEL);
191 108
                 };
192 109
 
193 110
                 DataAccess.addDbo = function(data, luBusyGroups) {
194
-                    var model = DataAccess.initModel(data, DataAccess.OP_ADD);
195
-                    return DataAccess.thenInitDbo(DataAccess.post('', null, model, luBusyGroups));
111
+                    var model = DataAccess.modelInitializer.initData(data, DataAccess.OP_ADD);
112
+                    return DataAccess.dboInitializer.thenInitData(DataAccess.post('', null, model, luBusyGroups), DataAccess.TYPE_MODEL);
196 113
                 };
197 114
 
198 115
                 DataAccess.editSingleByIdDbo = function(id, data, luBusyGroups) {
199
-                    var model = DataAccess.initModel(data, DataAccess.OP_EDIT);
200
-                    return DataAccess.thenInitDbo(DataAccess.post(id, null, model, luBusyGroups));
116
+                    var model = DataAccess.modelInitializer.initData(data, DataAccess.OP_EDIT);
117
+                    return DataAccess.dboInitializer.thenInitData(DataAccess.post(id, null, model, luBusyGroups), DataAccess.TYPE_MODEL);
201 118
                 };
202 119
 
203 120
                 DataAccess.deleteDbo = function(id, luBusyGroups) {
204
-                    return DataAccess.thenInitDbo(DataAccess.delete(id, null, null, luBusyGroups));
121
+                    return DataAccess.dboInitializer.thenInitData(DataAccess.delete(id, null, null, luBusyGroups), DataAccess.TYPE_MODEL);
205 122
                 };
206 123
 
207 124
                 return DataAccess;

Loading…
Cancel
Save