forked from mleibman/SlickGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slick.remotemodel.js
161 lines (122 loc) · 3.44 KB
/
slick.remotemodel.js
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
(function($) {
/***
* A sample AJAX data store implementation.
* Right now, it's hooked up to load all Apple-related Digg stories, but can
* easily be extended to support and JSONP-compatible backend that accepts paging parameters.
*/
function RemoteModel() {
// private
var PAGESIZE = 50;
var data = {length:0};
var searchstr = "apple";
var sortcol = null;
var sortdir = 1;
var h_request = null;
var req = null; // ajax request
// events
var onDataLoading = new Slick.Event();
var onDataLoaded = new Slick.Event();
function init() {
}
function isDataLoaded(from,to) {
for (var i=from; i<=to; i++) {
if (data[i] == undefined || data[i] == null)
return false;
}
return true;
}
function clear() {
for (var key in data) {
delete data[key];
}
data.length = 0;
}
function ensureData(from,to) {
if (req) {
req.abort();
for (var i=req.fromPage; i<=req.toPage; i++)
data[i*PAGESIZE] = undefined;
}
if (from < 0)
from = 0;
var fromPage = Math.floor(from / PAGESIZE);
var toPage = Math.floor(to / PAGESIZE);
while (data[fromPage * PAGESIZE] !== undefined && fromPage < toPage)
fromPage++;
while (data[toPage * PAGESIZE] !== undefined && fromPage < toPage)
toPage--;
if (fromPage > toPage || ((fromPage == toPage) && data[fromPage*PAGESIZE] !== undefined)) {
// TODO: look-ahead
return;
}
var url = "http://services.digg.com/search/stories?query=" + searchstr + "&offset=" + (fromPage * PAGESIZE) + "&count=" + (((toPage - fromPage) * PAGESIZE) + PAGESIZE) + "&appkey=http://slickgrid.googlecode.com&type=javascript";
switch (sortcol) {
case "diggs":
url += ("&sort=" + ((sortdir>0)?"digg_count-asc":"digg_count-desc"));
break;
}
if (h_request != null)
clearTimeout(h_request);
h_request = setTimeout(function() {
for (var i=fromPage; i<=toPage; i++)
data[i*PAGESIZE] = null; // null indicates a 'requested but not available yet'
onDataLoading.notify({from:from, to:to});
req = $.jsonp({
url: url,
callbackParameter: "callback",
cache: true, // Digg doesn't accept the autogenerated cachebuster param
success: onSuccess,
error: function(){
onError(fromPage, toPage)
}
});
req.fromPage = fromPage;
req.toPage = toPage;
}, 50);
}
function onError(fromPage,toPage) {
alert("error loading pages " + fromPage + " to " + toPage);
}
function onSuccess(resp) {
var from = this.fromPage*PAGESIZE, to = from + resp.count;
data.length = parseInt(resp.total);
for (var i = 0; i < resp.stories.length; i++) {
data[from + i] = resp.stories[i];
data[from + i].index = from + i;
}
req = null;
onDataLoaded.notify({from:from, to:to});
}
function reloadData(from,to) {
for (var i=from; i<=to; i++)
delete data[i];
ensureData(from,to);
}
function setSort(column,dir) {
sortcol = column;
sortdir = dir;
clear();
}
function setSearch(str) {
searchstr = str;
clear();
}
init();
return {
// properties
"data": data,
// methods
"clear": clear,
"isDataLoaded": isDataLoaded,
"ensureData": ensureData,
"reloadData": reloadData,
"setSort": setSort,
"setSearch": setSearch,
// events
"onDataLoading": onDataLoading,
"onDataLoaded": onDataLoaded
};
}
// Slick.Data.RemoteModel
$.extend(true, window, { Slick: { Data: { RemoteModel: RemoteModel }}});
})(jQuery);