Skip to content

Commit 6a12a60

Browse files
committed
Experimental wrapper datasource for preloading
1 parent 7ada44f commit 6a12a60

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Photo Slideshow</title>
6+
<link rel="stylesheet" href="../themes/base/jquery.ui.all.css">
7+
<link rel="stylesheet" href="grid.css">
8+
<script src="../jquery-1.5.1.js"></script>
9+
<script src="../external/kite.js"></script>
10+
<script src="../ui/jquery.ui.core.js"></script>
11+
<script src="../ui/jquery.ui.widget.js"></script>
12+
<script src="../ui/jquery.ui.position.js"></script>
13+
<script src="../ui/jquery.ui.button.js"></script>
14+
<script src="datasource.js"></script>
15+
<script src="slideshow.js"></script>
16+
<script src="pager.js"></script>
17+
<script type="text/x-kite" id="pager-tmpl">
18+
<div class="controls">
19+
<button data-page="first">First</button>
20+
<button data-page="prev">Prev</button>
21+
<button data-page="prevStep">-1</button>
22+
<span>
23+
Page <input class="current" size="4"/>/<span class="total">0</span>,
24+
Total records <span class="totalRecords">0</span>
25+
</span>
26+
<button data-page="nextStep">+1</button>
27+
<button data-page="next">Next</button>
28+
<button data-page="last">Last</button>
29+
</div>
30+
</script>
31+
<script type="text/x-kite" id="photo-tmpl">
32+
<a href="{{href}}" title="{{title}}">
33+
<img src="{{src}}" width="{{width}}" height="{{height}}" alt="{{alt}}" />
34+
</a>
35+
</script>
36+
<script>
37+
$(function() {
38+
// TODO must be possible to set these through the datasource
39+
$.ajaxSetup({
40+
dataType: "jsonp",
41+
jsonpCallback: "jsonFlickrApi",
42+
});
43+
var flickr = $.dataSource({
44+
path: "http://api.flickr.com/services/rest/",
45+
urlMapper: function(path, queryParams, sort, filter, skip, take) {
46+
return path + "?" + $.param({
47+
format: "json",
48+
api_key: "d86848d57cb3f7ad94f2ee9a3c90eff1",
49+
method: "flickr.photos.search",
50+
tags: filter[0].filterValue,
51+
sort: "relevance",
52+
per_page: take,
53+
page: Math.ceil((skip || 0) / take + 1)
54+
});
55+
},
56+
resultsFilter: function (data) {
57+
this.totalCount = data.photos.total;
58+
return $.map( data.photos.photo, function( photo ) {
59+
return {
60+
src: kite( "http://farm{{farm}}.static.flickr.com/{{server}}/{{id}}_{{secret}}_m.jpg", photo ),
61+
href: kite( "http://www.flickr.com/photos/{{owner}}/{{id}}/", photo )
62+
}
63+
});
64+
}
65+
});
66+
67+
// wrap the original source to preload
68+
datasource = $.dataSource({
69+
path: "doesn't-matter-just-need-a-remote-source",
70+
paging: { take: 2 }
71+
});
72+
flickr._take = datasource._take * 3;
73+
datasource._refresh = function(options, completed) {
74+
flickr._filter = this._filter;
75+
76+
if (this._nextTake && this._nextTake.length) {
77+
this._items = this._nextTake.slice(0, datasource._take);
78+
this._nextTake = this._nextTake.slice(datasource._take);
79+
completed();
80+
return;
81+
}
82+
83+
flickr._skip = this._skip;
84+
// would be nice to be able to just pass this as a callback to refresh
85+
flickr.option("refresh", function() {
86+
datasource._items = flickr._items.slice(0, datasource._take);
87+
datasource._nextTake = flickr._items.slice(datasource._take);
88+
datasource.totalCount = flickr.totalCount;
89+
completed();
90+
}).refresh();
91+
};
92+
93+
$( "#slideshow" ).slideshow({
94+
source: datasource
95+
});
96+
97+
$( kite( "#pager-tmpl" )() ).insertBefore( "#slideshow" ).pager({
98+
source: datasource
99+
});
100+
101+
$( "#page-size" ).change(function() {
102+
datasource.option("paging", {
103+
take: +$(this).val(),
104+
// can't yet only set take
105+
skip: datasource._skip
106+
}).refresh();
107+
});
108+
109+
$( "#filter" ).change(function() {
110+
datasource.option("filter", {
111+
property: "tags",
112+
value: $(this).val()
113+
}).refresh();
114+
}).trigger("change");
115+
});
116+
117+
</script>
118+
</head>
119+
<body>
120+
121+
<h1>Photo Slideshow</h1>
122+
<p>features: paging</p>
123+
124+
<hr>
125+
126+
<p>
127+
Per page: <input id="page-size" value="2">
128+
</p>
129+
<p>
130+
Tags to look for (comma seperated): <input id="filter" value="jquery">
131+
</p>
132+
133+
134+
<div id="slideshow"></div>
135+
136+
</body>
137+
</html>

0 commit comments

Comments
 (0)