Skip to content

Commit 2e7b488

Browse files
committed
Grid SPF: Extract nested datasource and fix a few bugs, like filter not getting forwarded properly
1 parent 258c960 commit 2e7b488

File tree

2 files changed

+66
-61
lines changed

2 files changed

+66
-61
lines changed

grid-spf/datasource-nested.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
(function ($, undefined ) {
2+
3+
// TODO cache should be per-instance?
4+
var cache = {};
5+
$.widget( "ui.nestedDatasource", $.ui.datasource, {
6+
widgetEventPrefix: "datasource",
7+
options: {
8+
remote: null
9+
},
10+
11+
_create: function() {
12+
var options = this.options;
13+
options.source = function( request, response ) {
14+
var i, length,
15+
cached = true,
16+
tags = request.filter,
17+
pageSize = request.paging.limit,
18+
page = request.page,
19+
start = ( page - 1 ) * pageSize;
20+
21+
if ( !cache[ tags ] ) {
22+
cache[ tags ] = [];
23+
}
24+
25+
// check if we have cached data
26+
for ( i = start; i < start + pageSize; i++ ) {
27+
if ( !cache[ tags ][ i ] ) {
28+
cached = false;
29+
break;
30+
}
31+
}
32+
if ( cached ) {
33+
response( cache[ tags ].slice( start, start + pageSize ), cache[ tags ].total );
34+
return;
35+
}
36+
37+
options.remote.refresh(function() {
38+
var total = cache[ tags ].total = options.remote.totalCount;
39+
var data = options.remote.toArray();
40+
for ( i = 0, length = data.length; i < length; i++ ) {
41+
cache[ tags ][ start + i ] = data[ i ];
42+
}
43+
response( data.slice( 0, pageSize ), total );
44+
});
45+
};
46+
this._super( "_create" );
47+
},
48+
49+
_setOptions: function( options ) {
50+
if ( "filter" in options ) {
51+
this.options.remote.option( "filter", options.filter );
52+
}
53+
if ( "paging" in options ) {
54+
this.options.remote.option( "paging", {
55+
limit: ( "limit" in options.paging ?
56+
options.paging.limit : this.options.paging.limit ) * 5,
57+
offset: ( "offset" in options.paging ?
58+
options.paging.offset : this.options.paging.offset )
59+
});
60+
}
61+
this._super( "_setOptions", options );
62+
}
63+
});
64+
65+
})(jQuery);

grid-spf/photoslideshow_nested.html

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<script src="../ui/jquery.ui.position.js"></script>
1313
<script src="../ui/jquery.ui.button.js"></script>
1414
<script src="datasource.js"></script>
15+
<script src="datasource-nested.js"></script>
1516
<script src="slideshow.js"></script>
1617
<script src="pager.js"></script>
1718
<script type="text/x-kite" id="pager-tmpl">
@@ -67,67 +68,6 @@
6768
}
6869
});
6970

70-
var cache = {};
71-
$.widget( "ui.nestedDatasource", $.ui.datasource, {
72-
widgetEventPrefix: "datasource",
73-
options: {
74-
remote: null
75-
},
76-
77-
_create: function() {
78-
var options = this.options;
79-
options.source = function( request, response ) {
80-
var i, length,
81-
cached = true,
82-
tags = request.filter,
83-
pageSize = request.paging.limit,
84-
page = request.page,
85-
start = ( page - 1 ) * pageSize;
86-
87-
if ( !cache[ tags ] ) {
88-
cache[ tags ] = [];
89-
}
90-
91-
// check if we have cached data
92-
for ( i = start; i < start + pageSize; i++ ) {
93-
if ( !cache[ tags ][ i ] ) {
94-
cached = false;
95-
break;
96-
}
97-
}
98-
if ( cached ) {
99-
response( cache[ tags ].slice( start, start + pageSize ), cache[ tags ].total );
100-
return;
101-
}
102-
103-
options.remote.refresh(function() {
104-
var total = cache[ tags ].total = remote.totalCount;
105-
var data = remote.toArray();
106-
for ( i = 0, length = data.length; i < length; i++ ) {
107-
cache[ tags ][ start + i ] = data[ i ];
108-
}
109-
response( data.slice( 0, pageSize ), total );
110-
});
111-
};
112-
this._super( "_create" );
113-
},
114-
115-
_setOptions: function( options ) {
116-
if ( "filter" in options ) {
117-
remote.option( filter, options.filter );
118-
}
119-
if ( "paging" in options ) {
120-
remote.option( "paging", {
121-
limit: ( "limit" in options.paging ?
122-
options.paging.limit : this.options.paging.limit ) * 5,
123-
offset: ( "offset" in options.paging ?
124-
options.paging.offset : this.options.paging.offset )
125-
});
126-
}
127-
this._super( "_setOptions", options );
128-
}
129-
});
130-
13171
var datasource = $.ui.nestedDatasource({
13272
paging: {
13373
limit: 2

0 commit comments

Comments
 (0)