-
Notifications
You must be signed in to change notification settings - Fork 40
/
ember-parachute.js
220 lines (187 loc) · 6.16 KB
/
ember-parachute.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import Ember from 'ember';
import QueryParams from '../query-params';
import ParachuteEvent from '../-private/parachute-event';
import lookupController from '../utils/lookup-controller';
const {
RSVP,
run,
assign,
canInvoke,
tryInvoke,
sendEvent
} = Ember;
const {
keys
} = Object;
export function initialize(/* application */) {
if (Ember.Route._didInitializeParachute) {
return;
}
Ember.Route.reopen({
/**
* Setup the route's `queryParams` map and call the `setup` hook
* on the controller.
*
* @method setupController
* @public
* @param {Ember.Controller} controller
* @returns {void}
*/
setupController(controller, model, transition) {
this._super(...arguments);
if (QueryParams.hasParachute(controller)) {
this._setupParachuteQueryParamsMap(controller);
let { routeName } = this;
let event = new ParachuteEvent(routeName, controller, {});
// Overrides
event.changed = event.changes;
event.shouldRefresh = true;
tryInvoke(controller, 'setup', [event, transition]);
sendEvent(controller, 'setup', [event, transition]);
}
},
/**
* Call the `reset` hook on the controller.
*
* @method resetController
* @public
* @param {Ember.Controller} controller
* @param {Boolean} isExiting
* @returns {void}
*/
resetController(controller, isExiting) {
this._super(...arguments);
if (QueryParams.hasParachute(controller)) {
let { routeName } = this;
let event = new ParachuteEvent(routeName, controller, {});
// Overrides
event.shouldRefresh = false;
tryInvoke(controller, 'reset', [event, isExiting]);
sendEvent(controller, 'reset', [event, isExiting]);
}
},
/**
* For Engines support. `transition.handlerInfos` is used to compute
* the query params that will be injected into a controller. In lazily
* loaded engines, handlerInfos may be promises that don't contain the required
* information. Resolve them here to guarantee parachute can properly function.
*
* @method deserialize
* @param {Object} params the parameters extracted from the URL
* @param {Transition} transition
* @returns {Promise<any>} The model for this route
*/
deserialize(params, transition) {
// Check if handlers have already been loaded.
// If so, don't return a promise as it will result in
// the loading screen/state flashing.
if (!transition.handlerInfos.find(x => !x.handler)) {
return this._super(params, transition);
}
// Save and bind the refence to the super here
// as this._super doesn't work in callbacks
// https://github.com/emberjs/ember.js/issues/15291
const actualSuper = this._super.bind(this);
return RSVP.all(
transition.handlerInfos.map(x => x.handlerPromise)
).then(() => actualSuper(params, transition));
},
/**
* Serialize query param value if a given query param has a `serialize`
* method.
*
* @private
* @param {any} value
* @param {string} urlKey
* @returns {any}
*/
serializeQueryParam(value, urlKey/**, defaultValueType **/) {
let controller = lookupController(this);
if (QueryParams.hasParachute(controller)) {
let queryParam = QueryParams.lookupQueryParam(controller, urlKey);
if (canInvoke(queryParam, 'serialize')) {
return queryParam.serialize(value, controller);
}
}
return this._super(...arguments);
},
/**
* Deserialize query param value if a given query param has a `deserialize`
* method.
*
* @private
* @param {any} value
* @param {string} urlKey
* @returns {any}
*/
deserializeQueryParam(value, urlKey/**, defaultValueType **/) {
let controller = lookupController(this);
if (QueryParams.hasParachute(controller)) {
let queryParam = QueryParams.lookupQueryParam(controller, urlKey);
if (canInvoke(queryParam, 'deserialize')) {
return queryParam.deserialize(value, controller);
}
}
return this._super(...arguments);
},
/**
* Schedule a QueryParamChangeEvent when query params change.
*
* @private
* @param {string} routeName
* @param {Ember.Controller} controller
* @param {object} [changed={}]
* @returns {void}
*/
_scheduleParachuteChangeEvent(routeName, controller, changed = {}) {
run.schedule('afterRender', this, () => {
let event = new ParachuteEvent(routeName, controller, changed);
tryInvoke(controller, 'queryParamsDidChange', [event]);
sendEvent(controller, 'queryParamsDidChange', [event]);
});
},
/**
* Setup the route's `queryParams` map if it doesnt already exist from
* the controller's Parachute meta.
*
* @method _setupParachuteQueryParamsMap
* @private
* @param {Ember.Controller} controller
* @returns {void}
*/
_setupParachuteQueryParamsMap(controller) {
if (!this.__hasSetupParachuteQPs) {
let qpMap = this.get('queryParams');
let { qpMapForRoute } = QueryParams.metaFor(controller);
keys(qpMapForRoute).forEach(key => {
qpMapForRoute[key] = assign({}, qpMapForRoute[key], qpMap[key]);
});
this.set('queryParams', qpMapForRoute);
this.__hasSetupParachuteQPs = true;
}
},
actions: {
/**
* Route hook that fires when query params are changed.
*
* @public
* @param {object} [changed={}]
* @param {object} [present={}]
* @param {object} [removed={}]
* @returns {any}
*/
queryParamsDidChange(changed = {}, present = {}, removed = {}) {
let { controller, routeName } = this;
if (QueryParams.hasParachute(controller)) {
this._scheduleParachuteChangeEvent(routeName, controller, assign({}, changed, removed));
}
return this._super(...arguments);
}
}
});
Ember.Route.reopenClass({ _didInitializeParachute: true })
}
export default {
name: 'ember-parachute',
initialize
};