@@ -22,6 +22,16 @@ const EVENT_DELETED = `${EVENT_ROOT}.deleted`;
22
22
* @class
23
23
*/
24
24
export default class Provider extends Emitter {
25
+ /**
26
+ * Create a provider, with a model constructor and optionally
27
+ * a base URL for requests. If no model is provided then
28
+ * Object types will be emitted. Add event listeners for
29
+ * mvc.provider.{started,completed,added,changed,deleted} to
30
+ * tap into the request lifecycle and mvc.provider.error to
31
+ * deal with request errors.
32
+ * @param {Model } constructor - The model used to create objects from data.
33
+ * @param {string } origin - The base URL used for making requests.
34
+ */
25
35
constructor ( constructor , origin ) {
26
36
super ( ) ;
27
37
this . $origin = origin || '' ;
@@ -30,6 +40,14 @@ export default class Provider extends Emitter {
30
40
this . $timer = null ;
31
41
}
32
42
43
+ /**
44
+ * Request data from a remote source, either once or by interval.
45
+ * Subsequent calls to this function will cancel any existing
46
+ * timers.
47
+ * @param {string } url - The endpoint of the data provider.
48
+ * @param {Object } req - Request data. See the documentaton for fetch.
49
+ * @param {number } interval - If provided, the number of milliseconds between each request.
50
+ */
33
51
request ( url , req , interval ) {
34
52
this . cancel ( ) ;
35
53
if ( ! this . $timer ) {
@@ -40,6 +58,18 @@ export default class Provider extends Emitter {
40
58
}
41
59
}
42
60
61
+ /**
62
+ * Perform a request without interrupting any existing request interval timer.
63
+ * @param {string } url - The endpoint of the data provider.
64
+ * @param {Object } req - Request data. See the documentaton for fetch.
65
+ */
66
+ do ( url , req ) {
67
+ this . $fetch ( url , req ) ;
68
+ }
69
+
70
+ /**
71
+ * Cancel any existing request interval timer.
72
+ */
43
73
cancel ( ) {
44
74
if ( this . $timer ) {
45
75
clearTimeout ( this . $timer ) ;
@@ -158,22 +188,30 @@ export default class Provider extends Emitter {
158
188
return changed ;
159
189
}
160
190
161
- // objects property returns all objects loaded by provider
191
+ /**
192
+ * Return all objects which are registered with the provider.
193
+ */
162
194
get objects ( ) {
163
195
return Array . from ( this . $objs . values ( ) ) ;
164
196
}
165
197
166
- // keys property returns all keys
198
+ /**
199
+ * Return all object keys which are registered with the provider.
200
+ */
167
201
get keys ( ) {
168
202
return Array . from ( this . $objs . keys ( ) ) ;
169
203
}
170
204
171
- // objectForKey returns an object for specific key
205
+ /**
206
+ * Return an object which is registered with a key.
207
+ */
172
208
objectForKey ( key ) {
173
209
return this . $objs . get ( key ) ;
174
210
}
175
211
176
- // clear removes all objects from the provider
212
+ /**
213
+ * Remove any registered objects.
214
+ */
177
215
clear ( ) {
178
216
this . $objs . clear ( ) ;
179
217
}
0 commit comments