Browse Source

auto convert createdAt/updatedAt to Date

tags/v0.1.6
Robin Thoni 7 years ago
parent
commit
970cdaa260

+ 14
- 2
src/Business/lu-webapi-crud-business.js View File

@@ -37,12 +37,24 @@
37 37
                     return dst;
38 38
                 };
39 39
 
40
-                Business.initDbo = function(dbo)
40
+                Business._initDbo = function(dbo)
41 41
                 {
42 42
                     if (dbo == null) {
43 43
                         return null;
44 44
                     }
45
-                    return Business.extendDeep({}, Business.defaultDbo, dbo);
45
+                    dbo = Business.extendDeep({}, Business.defaultDbo, dbo);
46
+                    if (dbo.createdAt != null) {
47
+                        dbo.createdAt = new Date(dbo.createdAt);
48
+                    }
49
+                    if (dbo.updatedAt != null) {
50
+                        dbo.updatedAt = new Date(dbo.updatedAt);
51
+                    }
52
+                    return dbo;
53
+                };
54
+
55
+                Business.initDbo = function(dbo)
56
+                {
57
+                    return Business._initDbo(dbo);
46 58
                 };
47 59
 
48 60
                 Business.initListDbo = function(list)

+ 78
- 0
tests/Business/lu-webapi-crud-business.spec.js View File

@@ -37,6 +37,28 @@ describe('lu-webapi-crud-business factory', function() {
37 37
                         }
38 38
                     });
39 39
                 }
40
+                else if (id == 3) {
41
+                    deferred.resolve({
42
+                        id: id,
43
+                        name: "Test.2",
44
+                        obj: {
45
+                            value: 4242
46
+                        },
47
+                        createdAt: "2016-12-27T05:00:00-05:00",
48
+                        updatedAt: null
49
+                    });
50
+                }
51
+                else if (id == 4) {
52
+                    deferred.resolve({
53
+                        id: id,
54
+                        name: "Test.2",
55
+                        obj: {
56
+                            value: 4242
57
+                        },
58
+                        createdAt: "2016-12-27T05:00:00-05:00",
59
+                        updatedAt: "2016-12-28T05:00:00-05:00"
60
+                    });
61
+                }
40 62
                 return deferred.promise;
41 63
             },
42 64
             getMultiple: function (orderBy, filter, page, perPage, luBusyGroups) {
@@ -226,4 +248,60 @@ describe('lu-webapi-crud-business factory', function() {
226 248
         });
227 249
         $rootScope.$digest();
228 250
     });
251
+
252
+
253
+    it('should convert createdAt into Date', function()
254
+    {
255
+        var business = luWebApiCrudBusiness.create(fakeDataAccess);
256
+        business.defaultDbo = {
257
+            id: null,
258
+            name: "",
259
+            createdAt: null,
260
+            updatedAt: null,
261
+            dummyFunction: function () {
262
+                return '_' + this.name + '_';
263
+            }
264
+        };
265
+
266
+        var id = 3;
267
+        business.getSingleById(id, null).then(function (data) {
268
+            expect(data.id).toEqual(id);
269
+            expect(data.name).toEqual("Test.2");
270
+            expect(data.dummyFunction).not.toBeNull();
271
+            expect(data.dummyFunction()).toEqual("_Test.2_");
272
+            expect(data.createdAt).toEqual(new Date("2016-12-27T05:00:00-05:00"));
273
+            expect(data.updatedAt).toBeNull();
274
+        }, function (error) {
275
+            expect(error).toBeNull();
276
+        });
277
+        $rootScope.$digest();
278
+    });
279
+
280
+
281
+    it('should convert createdAt and updatedAt into Date', function()
282
+    {
283
+        var business = luWebApiCrudBusiness.create(fakeDataAccess);
284
+        business.defaultDbo = {
285
+            id: null,
286
+            name: "",
287
+            createdAt: null,
288
+            updatedAt: null,
289
+            dummyFunction: function () {
290
+                return '_' + this.name + '_';
291
+            }
292
+        };
293
+
294
+        var id = 4;
295
+        business.getSingleById(id, null).then(function (data) {
296
+            expect(data.id).toEqual(id);
297
+            expect(data.name).toEqual("Test.2");
298
+            expect(data.dummyFunction).not.toBeNull();
299
+            expect(data.dummyFunction()).toEqual("_Test.2_");
300
+            expect(data.createdAt).toEqual(new Date("2016-12-27T05:00:00-05:00"));
301
+            expect(data.updatedAt).toEqual(new Date("2016-12-28T05:00:00-05:00"));
302
+        }, function (error) {
303
+            expect(error).toBeNull();
304
+        });
305
+        $rootScope.$digest();
306
+    });
229 307
 });

Loading…
Cancel
Save