Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle multiple queryParams created by applying multiple mixins #79

Merged
merged 2 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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