forked from jquery/jquery-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataview-preloader.js
More file actions
83 lines (75 loc) · 1.97 KB
/
dataview-preloader.js
File metadata and controls
83 lines (75 loc) · 1.97 KB
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
/*
* Preloader Dataview
*
* Depends on:
* dataview
*/
(function ($, undefined ) {
$.widget( "ui.preloaderDataview", $.ui.dataview, {
widgetEventPrefix: "dataview",
options: {
remote: null
},
_create: function() {
var options = this.options;
var cache = {};
options.source = function( request, response ) {
var i, length,
cached = true,
tags = request.filter,
pageSize = request.paging.limit,
page = request.page,
start = ( page - 1 ) * pageSize,
lastPage = start + pageSize === (cache[ tags ] && cache[ tags ].length);
if ( !cache[ tags ] ) {
cache[ tags ] = [];
}
// check if we have cached data
for ( i = start; i < start + pageSize; i++ ) {
if ( !cache[ tags ][ i ] ) {
cached = false;
break;
}
}
if ( cached ) {
response( cache[ tags ].slice( start, start + pageSize ), cache[ tags ].total );
// let through to preload next batch when on last page
if ( !lastPage ) {
return;
}
}
options.remote.refresh(function() {
var total = cache[ tags ].total = options.remote.totalCount;
var data = options.remote.result;
for ( i = 0, length = data.length; i < length; i++ ) {
cache[ tags ][ start + i ] = data[ i ];
}
if ( !cached && !lastPage ) {
response( data.slice( 0, pageSize ), total );
}
});
};
this._super();
},
_setOptions: function( options ) {
if ( "filter" in options ) {
this.options.remote.option( "filter", options.filter );
}
if ( "sort" in options ) {
this.options.remote.option( "sort", options.sort );
// throw away the cache and start over
cache = {};
this.option( "paging.offset", 0 );
}
if ( "paging" in options ) {
this.options.remote.option( "paging", {
limit: ( "limit" in options.paging ?
options.paging.limit : this.options.paging.limit ) * 5,
offset: ( "offset" in options.paging ?
options.paging.offset : this.options.paging.offset )
});
}
this._super( options );
}
});
})(jQuery);