UNPKG

8.03 kBJavaScriptView Raw
1(function ($) {
2 /***
3 * A sample AJAX data store implementation.
4 * Right now, it's hooked up to load Hackernews stories, but can
5 * easily be extended to support any JSONP-compatible backend that accepts paging parameters.
6 */
7 function RemoteModel() {
8 // private
9 var PAGESIZE = 10;
10 var data = {length: 0};
11 var h_request = null;
12 var req = null; // ajax request
13
14 // events
15 var onDataLoading = new Slick.Event();
16 var onDataLoaded = new Slick.Event();
17
18
19 function init() {
20 }
21
22
23 function isDataLoaded(from, to) {
24 for (var i = from; i <= to; i++) {
25 if (data[i] == undefined || data[i] == null) {
26 return false;
27 }
28 }
29
30 return true;
31 }
32
33
34 function clear() {
35 for (var key in data) {
36 delete data[key];
37 }
38 data.length = 0;
39 }
40
41 function ensureData(from, to) {
42 if (req) {
43 req.abort();
44 for (var i = req.fromPage; i <= req.toPage; i++) {
45 data[i * PAGESIZE] = undefined;
46 }
47 }
48
49 if (from < 0) {
50 from = 0;
51 }
52
53 if (data.length > 0) {
54 to = Math.min(to, data.length - 1);
55 }
56
57 var fromPage = Math.floor(from / PAGESIZE);
58 var toPage = Math.floor(to / PAGESIZE);
59
60 while (data[fromPage * PAGESIZE] !== undefined && fromPage < toPage)
61 fromPage++;
62
63 while (data[toPage * PAGESIZE] !== undefined && fromPage < toPage)
64 toPage--;
65
66 if (fromPage > toPage || ((fromPage == toPage) && data[fromPage * PAGESIZE] !== undefined)) {
67 // TODO: look-ahead
68 onDataLoaded.notify({from: from, to: to});
69 return;
70 }
71
72 var recStart = (fromPage * PAGESIZE);
73 var recCount = (((toPage - fromPage) * PAGESIZE) + PAGESIZE);
74
75 var url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss"
76 + "(" + recStart + "%2C" + recCount + ")"
77 + "%20where%20url%3D%22http%3A%2F%2Frss.news.yahoo.com%2Frss%2Ftopstories%22"
78 + "&format=json";
79
80 if (h_request != null) {
81 clearTimeout(h_request);
82 }
83
84 h_request = setTimeout(function () {
85 for (var i = fromPage; i <= toPage; i++)
86 data[i * PAGESIZE] = null; // null indicates a 'requested but not available yet'
87
88 onDataLoading.notify({from: from, to: to});
89
90 req = $.jsonp({
91 url: url,
92 callbackParameter: "callback",
93 cache: true,
94 success: function (json, textStatus, xOptions) {
95 onSuccess(json, recStart);
96 },
97 error: function () {
98 onError(fromPage, toPage);
99 }
100 });
101
102 req.fromPage = fromPage;
103 req.toPage = toPage;
104 }, 50);
105 }
106
107
108 function onError(fromPage, toPage) {
109 alert("error loading pages " + fromPage + " to " + toPage);
110 }
111
112// SAMPLE DATA
113// {
114// "query": {
115// "count": 40,
116// "created": "2015-03-03T00:34:00Z",
117// "lang": "en-US",
118// "results": {
119// "item": [
120// {
121// "title": "Netanyahu assails Iran deal, touts US-Israel ties",
122// "description": "<p><a href=\"http://news.yahoo.com/netanyahu-us-officials-face-off-iran-133539021--politics.html\"><img src=\"http://l2.yimg.com/bt/api/res/1.2/4eoBxbJStrbGAKbmBYOJfg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/2f3a20c2d46d9f096f0f6a706700d430.jpg\" width=\"130\" height=\"86\" alt=\"Israeli Prime Minister Benjamin Netanyahu addresses the 2015 American Israel Public Affairs Committee (AIPAC) Policy Conference in Washington, Monday, March 2, 2015. (AP Photo/Cliff Owen)\" align=\"left\" title=\"Israeli Prime Minister Benjamin Netanyahu addresses the 2015 American Israel Public Affairs Committee (AIPAC) Policy Conference in Washington, Monday, March 2, 2015. (AP Photo/Cliff Owen)\" border=\"0\" /></a>WASHINGTON (AP) — Seeking to lower tensions, Benjamin Netanyahu and U.S. officials cast their dispute over Iran as a family squabble on Monday, even as the Israeli leader claimed President Barack Obama did not — and could not — fully understand his nation&#039;s vital security concerns.</p><br clear=\"all\"/>",
123// "link": "http://news.yahoo.com/netanyahu-us-officials-face-off-iran-133539021--politics.html",
124// "pubDate": "Mon, 02 Mar 2015 19:17:36 -0500",
125// "source": {
126// "url": "http://www.ap.org/",
127// "content": "Associated Press"
128// },
129// "guid": {
130// "isPermaLink": "false",
131// "content": "netanyahu-us-officials-face-off-iran-133539021--politics"
132// },
133// "content": {
134// "height": "86",
135// "type": "image/jpeg",
136// "url": "http://l2.yimg.com/bt/api/res/1.2/4eoBxbJStrbGAKbmBYOJfg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/2f3a20c2d46d9f096f0f6a706700d430.jpg",
137// "width": "130"
138// },
139// "text": {
140// "type": "html",
141// "content": "<p><a href=\"http://news.yahoo.com/netanyahu-us-officials-face-off-iran-133539021--politics.html\"><img src=\"http://l2.yimg.com/bt/api/res/1.2/4eoBxbJStrbGAKbmBYOJfg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/2f3a20c2d46d9f096f0f6a706700d430.jpg\" width=\"130\" height=\"86\" alt=\"Israeli Prime Minister Benjamin Netanyahu addresses the 2015 American Israel Public Affairs Committee (AIPAC) Policy Conference in Washington, Monday, March 2, 2015. (AP Photo/Cliff Owen)\" align=\"left\" title=\"Israeli Prime Minister Benjamin Netanyahu addresses the 2015 American Israel Public Affairs Committee (AIPAC) Policy Conference in Washington, Monday, March 2, 2015. (AP Photo/Cliff Owen)\" border=\"0\" /></a>WASHINGTON (AP) — Seeking to lower tensions, Benjamin Netanyahu and U.S. officials cast their dispute over Iran as a family squabble on Monday, even as the Israeli leader claimed President Barack Obama did not — and could not — fully understand his nation&#039;s vital security concerns.</p><br clear=\"all\"/>"
142// },
143// "credit": {
144// "role": "publishing company"
145// }
146// },
147// {... },
148// {... },
149// ]
150// }
151// }
152// }
153
154 function onSuccess(json, recStart) {
155 var recEnd = recStart;
156 if (json.query.count > 0) {
157 var results = json.query.results.item;
158 recEnd = recStart + results.length;
159 data.length = 100;// Math.min(parseInt(results.length),1000); // limitation of the API
160
161 for (var i = 0; i < results.length; i++) {
162 var item = results[i];
163
164 item.pubDate = new Date(item.pubDate);
165
166 data[recStart + i] = { index: recStart + i };
167 data[recStart + i].pubDate = item.pubDate;
168 data[recStart + i].title = item.title;
169 data[recStart + i].url = item.link;
170 data[recStart + i].text = item.description;
171 }
172 }
173 req = null;
174
175 onDataLoaded.notify({from: recStart, to: recEnd});
176 }
177
178
179 function reloadData(from, to) {
180 for (var i = from; i <= to; i++)
181 delete data[i];
182
183 ensureData(from, to);
184 }
185
186 init();
187
188 return {
189 // properties
190 "data": data,
191
192 // methods
193 "clear": clear,
194 "isDataLoaded": isDataLoaded,
195 "ensureData": ensureData,
196 "reloadData": reloadData,
197
198 // events
199 "onDataLoading": onDataLoading,
200 "onDataLoaded": onDataLoaded
201 };
202 }
203
204 // Slick.Data.RemoteModel
205 $.extend(true, window, { Slick: { Data: { RemoteModel: RemoteModel }}});
206})(jQuery);