Skip to content

Commit

Permalink
feat: pass controller to serialize / deserialize (#50)
Browse files Browse the repository at this point in the history
* feat: pass controller to `serialize` / `deserialize`

* test(query-params): assert that the controller is passed to `serialize` / `deserialize`
  • Loading branch information
buschtoens authored and offirgolan committed Jan 19, 2018
1 parent db02625 commit 26b1f43
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion addon/-private/parachute-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class ParachuteEvent {
*/
this.changed = queryParamsArray.reduce((changedParams, qp) => {
if (changedKeys.includes(qp.as)) {
changedParams[qp.key] = canInvoke(qp, 'deserialize') ? qp.deserialize(changed[qp.as]) : changed[qp.as];
changedParams[qp.key] = canInvoke(qp, 'deserialize') ? qp.deserialize(changed[qp.as], controller) : changed[qp.as];
}
return changedParams;
}, {}, undefined);
Expand Down
2 changes: 1 addition & 1 deletion addon/-private/query-param.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class QueryParam {
*/
serializedValue(controller) {
const value = this.value(controller);
return canInvoke(this, 'serialize') ? this.serialize(value) : value;
return canInvoke(this, 'serialize') ? this.serialize(value, controller) : value;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions addon/initializers/ember-parachute.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function initialize(/* application */) {
let queryParam = QueryParams.lookupQueryParam(controller, urlKey);

if (canInvoke(queryParam, 'serialize')) {
return queryParam.serialize(value);
return queryParam.serialize(value, controller);
}
}

Expand All @@ -111,7 +111,7 @@ export function initialize(/* application */) {
let queryParam = QueryParams.lookupQueryParam(controller, urlKey);

if (canInvoke(queryParam, 'deserialize')) {
return queryParam.deserialize(value);
return queryParam.deserialize(value, controller);
}
}

Expand Down
12 changes: 10 additions & 2 deletions tests/unit/query-params-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ const queryParams = new QueryParams({
colors: {
defaultValue: [],
refresh: true,
serialize(value) {
serialize(value, controller) {
assertController(controller);
return value.toString();
},
deserialize(value = '') {
deserialize(value = '', controller) {
assertController(controller);
return value.split(',');
}
}
Expand All @@ -46,6 +48,12 @@ const defaultValues = {
const Controller = Ember.Object.extend(queryParams.Mixin);
let controller;

function assertController(controller) {
if (!(controller instanceof Controller)) {
throw new Error('Expected the controller to be passed as the second parameter to serialize / deserialize.');
}
}

module('Unit | QueryParams', {
beforeEach() {
controller = Controller.create();
Expand Down

0 comments on commit 26b1f43

Please sign in to comment.