-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfiniteLoader.js
141 lines (118 loc) · 3.25 KB
/
InfiniteLoader.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
define([
'nbd/Class',
'nbd/trait/pubsub',
'nbd/util/construct',
'nbd/util/extend',
'lib/infinitescroll',
'lib/xhr'
], function(Class, pubsub, construct, extend, infinitescroll, xhr) {
'use strict';
return Class.extend({
init: function(context, offset) {
this.context = context || 'window';
this.resetParams(offset);
},
breakpoint: 1,
offsetKey: 'offset',
offset: 0,
data: {},
url: undefined,
_infinitescroll: infinitescroll,
_xhr: xhr,
hasMoreResults: function(response) {
throw "InfiniteLoader requires a 'hasMoreResults(response)' function. Please extend and implement.";
},
getNextOffset: function(response) {
throw "InfiniteLoader requires a 'getNextOffset(response)' function. Please extend and implement.";
},
loaded: function(response) {
throw "InfiniteLoader requires a 'loaded(response)' function. Please extend and implement.";
},
resetParams: function(offset, data, url) {
delete this.offset;
delete this.data;
delete this.url;
return this.setParams(offset, data, url);
},
setParams: function(offset, data, url) {
if (offset != null) {
this.offset = offset;
}
if (data != null) {
this.data = data;
}
if (url != null) {
this.url = url;
}
return this;
},
bind: function() {
if (this._boundLoad) {
return;
}
this._boundLoad = this.load.bind(this);
this._infinitescroll(this.breakpoint, this._boundLoad, this.context);
return this;
},
_stop: function() {
if (this._request) {
this._request.abort();
}
},
unbind: function() {
if (!this._boundLoad) {
return;
}
this._infinitescroll.remove(this._boundLoad, this.context);
delete this._boundLoad;
return this;
},
load: function() {
this.trigger('before');
this._request = this._xhr(this._xhrOptions());
// Need to split up the chain at this point
var chain = this._request.then(this.loaded.bind(this));
// Event out only the relevant events
chain.then(
this.trigger.bind(this, 'success'),
this.trigger.bind(this, 'error')
);
return this._request
.then(this._trackState.bind(this))
.then(function() {
// Rejoin original chain
return chain;
});
},
_xhrOptions: function() {
var data = typeof this.data === 'function' ? this.data() : this.data;
data = extend({}, data);
data[this.offsetKey] = this.offset;
return {
url: this.url,
data: data
};
},
_trackState: function(response) {
this.offset = this.getNextOffset(response);
if (!this.hasMoreResults(response)) {
throw 'No more results';
}
return response;
},
reload: function(offset, data, url) {
this.resetParams(offset, data, url);
this._stop();
this.unbind();
// Make the initial request after the reset
var request = this.load();
request.then(this.bind.bind(this));
return request;
}
}, {
init: function(context, offset) {
return construct.apply(this, arguments).bind();
}
})
.mixin(pubsub);
});