forked from platanus/angular-restmod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaged.js
More file actions
49 lines (45 loc) · 1.52 KB
/
Copy pathpaged.js
File metadata and controls
49 lines (45 loc) · 1.52 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
/**
* @mixin PagedModel
*
* @description Extracts header paging information into the `$page` and `$pageCount` properties.
*
* Usage:
*
* Just add mixin to a model's mixin chain
*
* ```javascript
* var Bike = restmod.model('api/bikes', 'PagedModel');
* ```
*
* Then using fetch on a collection bound to a paged api should provide page information
*
* ```javascript
* Bike.$search().$then(function() {
* console.log this.$page; // should print the current page number.
* console.log this.$pageCount; // should print the request total page count.
* });
* ```
*
* Paging information is extracted from the 'X-Page' and the 'X-Page-Total' headers, to use different headers just
* override the $pageHeader or the $pageCountHeader definition during model building.
*
* ```javascript
* restmod.model('PagedModel', function() {
* this.define('$pageHeader', 'X-My-Page-Header');
* })
* ```
*
*/
'use strict';
angular.module('restmod').factory('PagedModel', ['restmod', function(restmod) {
return restmod.mixin(function() {
this.setProperty('pageHeader', 'X-Page')
.setProperty('pageCountHeader', 'X-Page-Total')
.on('after-fetch-many', function(_response) {
var page = _response.headers(this.$type.getProperty('pageHeader')),
pageCount = _response.headers(this.$type.getProperty('pageCountHeader'));
this.$page = (page !== undefined ? parseInt(page, 10) : 1);
this.$pageCount = (pageCount !== undefined ? parseInt(pageCount, 10) : 1);
});
});
}]);