This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
indexeddb-ajax.html
122 lines (117 loc) · 3.74 KB
/
indexeddb-ajax.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../core-ajax/core-ajax.html">
<polymer-element hidden
name="indexeddb-ajax"
attributes="url withCredentials start length search sortColumn sortDescending columns data dataSize loading dbVersion dbName dbStoreName"
>
<template>
<core-ajax id="ajax" withCredentials="{{withCredentials}}" method="GET" handleAs="json" url="{{url}}" on-core-response="{{ajaxResponse}}"></core-ajax>
</template>
<script>
"use strict";
Polymer({
url: null,
withCredentials: false,
start: 0,
length: -1,
search: "",
sortColumn: null,
sortDescending: false,
db: null,
loading: false,
openTransactionCount: 0,
created: function(){
if(!this.columns) this.columns = [];
},
dbVersion: 1,
dbName: "default",
dbStoreName: "default",
json: "",
ready: function() {
var self = this;
var request = indexedDB.open(this.dbName, this.dbVersion);
request.onupgradeneeded = function(event) {
var db = event.target.result;
self.$.ajax.xhrArgs = { sync: true };
self.$.ajax.go();
var response = self.upgradeXHR.response;
delete self.upgradeXHR;
if(db.objectStoreNames.contains(self.dbStoreName)) db.deleteObjectStore(self.dbStoreName);
var objectStore = db.createObjectStore(self.dbStoreName, { autoIncrement: true });
var columns = self.columns;
if(columns.length === 0 && response[0]){
var head = response[0];
for (var prop in head) {
if(head.hasOwnProperty(prop)){
objectStore.createIndex(prop, prop, { unique: false });
}
}
}else{
columns.forEach(function(element){
objectStore.createIndex(element.name, element.name, { unique: false });
});
}
objectStore.transaction.oncomplete = function(event) {
// Store values in the newly created objectStore.
var innerObjectStore = db.transaction(self.dbStoreName, "readwrite").objectStore(self.dbStoreName);
response.forEach(function(element){
innerObjectStore.add(element);
});
self.db = db;
}
};
request.onsuccess = function(event) {
self.db = request.result;
self.job('go',self.go);
};
},
ajaxResponse: function(e,o){
this.upgradeXHR = o;
},
go: function(){
if(this.db){
if(this.columns.length>0){
var self = this;
var objectStore = this.db.transaction([self.dbStoreName], "readonly").objectStore(self.dbStoreName);
var index;
if(this.sortColumn) objectStore = objectStore.index(this.sortColumn);
var skip = this.start;
var length = this.length;
var results = [];
var direction = this.sortDescending ? "prev" : "next";
this.openTransactionCount += 2;
if(!this.loading) this.loading = true;
objectStore.count().onsuccess = function(event){
self.dataSize = event.target.result;
self.openTransactionCount--;
if(self.openTransactionCount === 0) self.loading = false;
}
objectStore.openCursor(null, direction).onsuccess = function(event) {
var cursor = event.target.result;
if (cursor && length !== 0) {
if(skip>0){
cursor.advance(skip);
skip = 0;
}else{
length--;
var obj = cursor.value;
results.push(obj);
cursor.continue();
}
}else{
self.data = results;
self.openTransactionCount--;
if(self.openTransactionCount === 0) self.loading = false;
}
};
};
}else this.job('go',this.go);
},
columnsChanged: function(){ this.job('go',this.go); },
startChanged: function(){ this.job('go',this.go); },
lengthChanged: function(){ this.job('go',this.go); },
sortColumnChanged: function(){ this.job('go',this.go); },
sortDescendingChanged: function(){ this.job('go',this.go); }
});
</script>
</polymer-element>