Skip to content

Commit

Permalink
fix: Handle multiple queryParams created by applying multiple mixins (o…
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored Mar 6, 2019
1 parent f511051 commit f4abe44
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 29 deletions.
1 change: 0 additions & 1 deletion .tool-versions

This file was deleted.

6 changes: 6 additions & 0 deletions addon/-private/parachute-meta.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { A as emberArray } from '@ember/array';
import QueryParam from './query-param';
import { PARACHUTE_QPS } from './symbols';

const { keys } = Object;

Expand Down Expand Up @@ -36,6 +37,11 @@ export default class ParachuteMeta {
{}
);

// Meta info used by the decorators
Object.defineProperty(this.qpMapForController, PARACHUTE_QPS, {
value: true
});

this.qpMapForRoute = this.queryParamsArray.reduce(
(qps, { key, replace }) => {
qps[key] = { replace };
Expand Down
1 change: 1 addition & 0 deletions addon/-private/symbols.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const HAS_PARACHUTE = '__has_parachute__';
export const PARACHUTE_META = '__parachute_meta__';
export const PARACHUTE_QPS = '__parachute_qps__';
17 changes: 17 additions & 0 deletions addon/decorators/query-param.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { A } from '@ember/array';
import { PARACHUTE_QPS } from 'ember-parachute/-private/symbols';
import {
addQueryParamFor,
getQueryParamsFor
Expand All @@ -12,6 +14,21 @@ function createDescriptor(desc, qpDefinition) {
addQueryParamFor(klass, desc.key, qpDefinition);
klass.reopen(getQueryParamsFor(klass).Mixin);

const proto = klass.proto();

// Remove duplicate queryParams created by the multiple mixins
if (Array.isArray(proto.queryParams)) {
const queryParams = A([...proto.queryParams]);
const parachuteQueryParams = queryParams.filterBy(PARACHUTE_QPS, true);

// Keep the newest one
parachuteQueryParams.pop();
// Remove the old ones
queryParams.removeObjects(parachuteQueryParams);

proto.queryParams = queryParams.toArray();
}

return klass;
}
};
Expand Down
26 changes: 0 additions & 26 deletions addon/query-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { HAS_PARACHUTE, PARACHUTE_META } from './-private/symbols';
import ParachuteMeta from './-private/parachute-meta';
import queryParamsStateFor from './-private/state';


const { keys } = Object;

/**
Expand Down Expand Up @@ -250,30 +249,6 @@ export default class QueryParams {
}
).readOnly(),

/**
* Overridable hook that fires when query params change.
*
* @public
* @returns {void}
*/
queryParamsDidChange() {},

/**
* Overridable hook that fires after the route calls `setupController`
*
* @public
* @returns {void}
*/
setup() {},

/**
* Overridable hook that fires after the route calls `resetController`
*
* @public
* @returns {void}
*/
reset() {},

/**
* Reset query params to their default value. Accepts an optional array
* of query param keys to reset.
Expand All @@ -298,7 +273,6 @@ export default class QueryParams {
}
});


return ControllerMixin;
}
}
2 changes: 0 additions & 2 deletions tests/unit/decorators-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ module('Unit | Decorators', function() {
});

test('it works', function(assert) {
assert.ok(typeof controller.setup === 'function');
assert.ok(typeof controller.reset === 'function');
assert.ok(QueryParams.metaFor(controller));
});
});
Expand Down

0 comments on commit f4abe44

Please sign in to comment.