author | ymh <ymh.work@gmail.com> |
Fri, 04 Nov 2016 19:03:25 +0100 | |
changeset 392 | 4fbe94af93e8 |
parent 344 | 70451a4dc9ae |
child 394 | 48458e099b05 |
permissions | -rw-r--r-- |
42
7d091abf82fd
add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff
changeset
|
1 |
import Ember from 'ember'; |
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
2 |
import _ from 'lodash/lodash'; |
42
7d091abf82fd
add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff
changeset
|
3 |
|
7d091abf82fd
add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff
changeset
|
4 |
export default Ember.Route.extend({ |
194
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
5 |
|
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
6 |
filter: Ember.inject.service(), |
344 | 7 |
page: 1, |
8 |
limit: 10, |
|
9 |
||
10 |
documents: [], |
|
11 |
model: Ember.observer('page', function() { |
|
12 |
var self = this; |
|
13 |
var promise = this.store.query('document', { |
|
14 |
page: this.get('page'), |
|
15 |
perpage: this.get('limit') |
|
16 |
}); |
|
17 |
promise.then(function(value) { |
|
18 |
if(self.controller) { |
|
19 |
self.controller.set('page', self.get('page')); |
|
20 |
self.controller.set('documents', self.get('documents')); |
|
21 |
} |
|
22 |
self.set('documents', value); |
|
23 |
}); |
|
24 |
return promise; |
|
25 |
}), |
|
194
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
26 |
|
344 | 27 |
setupController: function(controller) { |
28 |
this._super(...arguments); |
|
29 |
controller.set('page', this.get('page')); |
|
30 |
controller.set('limit', this.get('limit')); |
|
31 |
controller.set('documents', this.get('documents')); |
|
32 |
}, |
|
33 |
||
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
34 |
/** |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
35 |
Serializes value of the query parameter based on defaultValueType |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
36 |
@method serializeQueryParam |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
37 |
@param {Object} value |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
38 |
@param {String} urlKey |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
39 |
@param {String} defaultValueType |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
40 |
@private |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
41 |
*/ |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
42 |
serializeQueryParam(value, urlKey, defaultValueType) { |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
43 |
if(_.contains(this.get('filter').get('queryParams'), urlKey)) { |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
44 |
return this.get('filter').serializeQueryParam(value, urlKey, defaultValueType); |
194
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
45 |
} |
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
46 |
return this._super(value, urlKey, defaultValueType); |
194
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
47 |
}, |
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
48 |
|
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
49 |
/** |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
50 |
Deserializes value of the query parameter based on defaultValueType |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
51 |
@method deserializeQueryParam |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
52 |
@param {Object} value |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
53 |
@param {String} urlKey |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
54 |
@param {String} defaultValueType |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
55 |
@private |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
56 |
*/ |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
57 |
deserializeQueryParam(value, urlKey, defaultValueType) { |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
58 |
if(_.contains(this.get('filter').get('queryParams'), urlKey)) { |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
59 |
return this.get('filter').deserializeQueryParam(value, urlKey, defaultValueType); |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
60 |
} |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
61 |
return this._super(value, urlKey, defaultValueType); |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
62 |
}, |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
63 |
|
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
64 |
|
194
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
65 |
actions: { |
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
66 |
|
344 | 67 |
setPageQueryparams: function(type) { |
68 |
var page = this.get('page'); |
|
69 |
if(type === 'previous') { |
|
70 |
page = page - 1; |
|
71 |
} else if(type === 'next') { |
|
72 |
page = page + 1; |
|
73 |
} |
|
74 |
this.propertyWillChange('page'); |
|
75 |
this.set('page', page); |
|
76 |
this.propertyDidChange('page'); |
|
77 |
}, |
|
78 |
||
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
79 |
|
194
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
80 |
willTransition: function() { |
199
b7c691c6179d
Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents:
194
diff
changeset
|
81 |
// Prevent navigation from removing query parameters |
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
344
diff
changeset
|
82 |
this.transitionTo({ queryParams: this.controller.get('queryParams') }); |
194
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
83 |
}, |
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
84 |
|
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
85 |
didTransition: function() { |
199
b7c691c6179d
Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents:
194
diff
changeset
|
86 |
// Append body classname depending on the route |
194
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
87 |
Ember.$('body').removeClass((this.controller.get('currentPath') || '').replace(/\//g, '-').dasherize()); |
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
88 |
Ember.run.once(this, function() { |
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
89 |
Ember.$('body').addClass((this.controller.get('currentPath') ||'').replace(/\//g, '-').dasherize()); |
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
90 |
}); |
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
91 |
} |
02c6aa9a99d7
Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents:
92
diff
changeset
|
92 |
|
81
848e4a5ad4d9
update date params to be an array of date instead of a string
nowmad@nowmads-macbook-pro.local
parents:
74
diff
changeset
|
93 |
} |
848e4a5ad4d9
update date params to be an array of date instead of a string
nowmad@nowmads-macbook-pro.local
parents:
74
diff
changeset
|
94 |
|
42
7d091abf82fd
add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff
changeset
|
95 |
}); |