-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathangular-db.js
More file actions
89 lines (79 loc) · 2.41 KB
/
angular-db.js
File metadata and controls
89 lines (79 loc) · 2.41 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
var dbService = angular.module('dbService', []);
dbService.provider('db', function() {
var self = this;
var options = null;
this.config = function(options) {
this.options = options;
};
this.$get = function($rootScope, $q) {
var deferred = $q.defer();
// TODO: this.options || default
var request = db.open(this.options);
var server = null;
var objectStore = null;
request.done(function(s) {
console.log('資料庫準備完成');
server = s;
deferred.resolve();
if ( ! $rootScope.$$phase) $rootScope.$apply();
});
var execute = function(transactionFunction) {
if ( ! server) {
deferred.promise.then(function() {
transactionFunction(server);
});
} else {
transactionFunction(server);
}
};
var get = function(key, value, onSuccess, onError) {
console.log('資料表查詢開始');
execute(function(connection) {
connection[self.objectStore]
.query()
.filter(key, value)
.execute()
.done(function(array) {
console.log('資料表查詢成功: ', array);
if (typeof onSuccess === 'function') onSuccess(array);
if ( ! $rootScope.$$phase) $rootScope.$apply();
});
});
};
var putBatch = function(array, onSuccess, onError) {
console.log('資料表更新開始');
execute(function(connection) {
connection[self.objectStore]
.update.apply(null, array)
.done(function(array) {
console.log('資料表更新成功: ', array);
if (typeof onSuccess === 'function') onSuccess(array);
if ( ! $rootScope.$$phase) $rootScope.$apply();
});
});
};
var getBatch = function(onSuccess, onError) {
console.log('資料表查詢開始');
execute(function(connection) {
connection[self.objectStore]
.query()
.filter()
.execute()
.done(function(array) {
console.log('資料表查詢成功: ', array);
if (typeof onSuccess === 'function') onSuccess(array);
if ( ! $rootScope.$$phase) $rootScope.$apply();
});
});
};
var service = function(objectStore) {
self.objectStore = objectStore;
return {
get: get,
putBatch: putBatch,
getBatch: getBatch
}
};
return service;
};
});