forked from ashteya/ionic-tutorial-lokijs
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbirthday.service.js
More file actions
383 lines (296 loc) · 10.7 KB
/
Copy pathbirthday.service.js
File metadata and controls
383 lines (296 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
var db, birthdays, fbA, tm, temp;
(function() {
angular.module('starter')
.factory('BirthdayService', ['$q', 'Loki', '$firebaseArray', '$timeout',
'$interval', '$ionicPlatform', BirthdayService
]);
function BirthdayService($q, Loki, $firebaseArray, $timeout, $interval, $ionicPlatform) {
var _db;
tm = $timeout;
var _birthdays;
var _alreadyLoad = null;
var _fbAlreadyLoad = false;
var _temp;
var _ref = new Firebase("https://scm-loki.firebaseio.com/");
var _bdRef = _ref.child('birthdays');
var _bd = $firebaseArray(_bdRef);
var wait = null;
var loading = null;
_bd.$loaded()
.then(function(res) {
_fbAlreadyLoad = true;
console.log('array loaded', res);
console.timeEnd('firebase')
})
.catch(function(err) {
console.log('cant loaded info from fb', err);
});
fbA = _bd;
/* tm = $timeout(function() {
console.log('timeout fn', tm);
tm = null;
console.log('timeout fn', tm);
}, 5000);
*/
// console.log('asignado tm', tm);
function initDB() {
var options = {
autosave: true,
autosaveInterval: 1000
};
if (ionic.Platform.isAndroid() || ionic.Platform.isIOS()) {
var fsAdapter = new LokiCordovaFSAdapter({
"prefix": "loki"
});
options.adapter = fsAdapter;
}
_db = new Loki('birthdaysDB', options);
db = _db;
}
function isDbLoad() {
return $q(function(resolve, reject) {
if (_alreadyLoad) {
resolve();
} else {
if (loading) {
validateLoadDb(resolve, reject);
} else {
loading = true;
$ionicPlatform.ready()
.then(function() {
console.info('platform ready');
initDB();
return true;
})
.then(function() {
_db.loadDatabase(options, function() {
console.timeEnd('loki');
_alreadyLoad = true;
loading = false;
validateBirthDayCollection();
validateTempCollection();
resolve();
});
}).catch(function(err) {
console.error('cant load db', err);
reject(err);
});
}
var options = {
birthdays: {
proto: Object,
inflate: function(src, dst) {
var prop;
for (prop in src) {
if (prop === 'Date') {
dst.Date = new Date(src.Date);
} else {
dst[prop] = src[prop];
}
}
}
}
};
}
});
}
function validateLoadDb(resolve, reject) {
if (_alreadyLoad) {
resolve();
} else {
$timeout(validateLoadDb, 20, false, resolve, reject);
}
}
function validateBirthDayCollection() {
_birthdays = _db.getCollection('birthdays');
if (!_birthdays) {
_birthdays = _db.addCollection('birthdays', {
indices: ['$id'],
clone: true
});
_birthdays.ensureUniqueIndex('$id');
}
var isEnabled = true;
_birthdays.setChangesApi(isEnabled);
birthdays = _birthdays;
}
function validateTempCollection() {
_temp = _db.getCollection('temp');
if (!_temp) {
_temp = _db.addCollection('temp', {
indices: ['$id'],
clone: true
});
_temp.ensureUniqueIndex('$id');
//ejemplo de como obtener el registro
// temp.by('$id',"-K5fLRn_P7GM1VceDQTa")
}
temp = _temp;
trySync();
}
function trySync() {
_temp.data.forEach(function(obj, i) {
console.log(obj, i);
var fbKey = obj.$id;
var objClone = angular.copy(obj);
if (delete objClone.$id && delete objClone.$loki) {
console.log('format obj', fbKey, objClone)
_bdRef.child(fbKey).set(objClone, syncCb(fbKey));
}
});
}
function syncCb(fbKey) {
function cbFb(error) {
if (error) {
console.error('error');
} else {
console.log('inserted to sync', fbKey);
/* $timeout(function() {
removeTempbyId(fbKey);
}, 100);*/
$timeout(removeTempbyId, 100, true, fbKey);
// removeTempbyId(fbKey);
}
}
return cbFb;
// body...
}
function getBirthdays() {
return isDbLoad()
.then(dbOK);
function dbOK() {
// service.emu = _birthdays.data;
if (_fbAlreadyLoad) {
return _bd;
} else {
return _birthdays.data;
}
}
}
function getTemp() {
return isDbLoad()
.then(dbOK);
function dbOK() {
return _temp.data;
}
}
function getAllBirthdays() {
return $q(function(resolve, reject) {
var options = {
birthdays: {
proto: Object,
inflate: function(src, dst) {
var prop;
for (prop in src) {
if (prop === 'Date') {
dst.Date = new Date(src.Date);
} else {
dst[prop] = src[prop];
}
}
}
}
};
_db.loadDatabase(options, function() {
_birthdays = _db.getCollection('birthdays');
_temp = _db.getCollection('temp');
if (!_birthdays) {
_birthdays = _db.addCollection('birthdays');
}
if (!_temp) {
_temp = _db.addCollection('temp', {
indices: ['$id']
});
_temp.ensureUniqueIndex('$id');
//ejemplo de como obtener el registro
// temp.by('$id',"-K5fLRn_P7GM1VceDQTa")
}
var isEnabled = true;
_birthdays.setChangesApi(isEnabled);
birthdays = _birthdays;
temp = _temp;
resolve(_birthdays.data);
});
});
};
function addBirthday(birthday) {
var before = _bd.length - 1;
var o = _bd.$add(birthday)
.then(oK)
.catch(eR);
console.log(_bd);
//TODO puedo asumir que el que ingreso siempre sera el ultimo???
validateIndex(before, birthday);
};
function validateIndex(before, birthday) {
var lastIndex = _bd.length - 1;
console.log('lastIndex', lastIndex, 'before', before, 'birthday', birthday);
if (lastIndex > before) {
console.log('ejecutada');
validateInserted(lastIndex, birthday);
} else {
console.log('otro timeout');
var tm = $timeout(validateIndex, 5, false, before, birthday);
}
}
function validateInserted(lastIndex, birthday) {
//TODO, POR QUE recien ccreado no me cogio el valor del array sera que necesita un timeuout??
var key = _bd.$keyAt(lastIndex);
birthday.$id = key;
/* // var birthday = _bd.$getRecord(key);
var newObj = {
fbKey: key,
fbVal: birthday
};
console.log(lastIndex, _bd[lastIndex], key, newObj);
_birthdays.insert(newObj);
_temp.insert(newObj);*/
_birthdays.insert(birthday);
_temp.insert(birthday);
}
function oK(ref) {
var fbKey = ref.key();
console.log('ok', fbKey, ref);
removeTempbyId(fbKey);
}
function eR(error) {
console.error(error);
}
function updateBirthday(birthday) {
_birthdays.update(birthday);
};
function deleteBirthday(birthday) {
_birthdays.remove(birthday);
};
function getBdArray() {
return _bd;
}
function removeTempbyId(fbKey) {
_temp.removeWhere({
'$id': fbKey
});
}
function syncFb() {
}
function getTempById(key) {
// $timeout(function() {
var obj = temp.by('$id', key);
// console.log('obj', obj);
return obj;
// }, 200);
}
var service = {
// emu: [],
initDB: initDB,
getAllBirthdays: getAllBirthdays,
addBirthday: addBirthday,
updateBirthday: updateBirthday,
deleteBirthday: deleteBirthday,
getBdArray: getBdArray,
getBirthdays: getBirthdays,
getTemp: getTemp,
getTempById: getTempById
};
return service;
}
})();