Skip to content

Instantly share code, notes, and snippets.

@mistahenry
Created December 12, 2018 14:17
Show Gist options
  • Save mistahenry/80d1b9edac21cfd75e84b8c63125d8cb to your computer and use it in GitHub Desktop.
Save mistahenry/80d1b9edac21cfd75e84b8c63125d8cb to your computer and use it in GitHub Desktop.
Explicit Query Params Help
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
export default Ember.Controller.extend({
queryParams: ['foo', 'bar'],
});
import {helper} from '@ember/component/helper';
import Object from '@ember/object';
import {assign} from '@ember/polyfills';
export function explicitQueryParams(params, hash) {
let values = assign({}, hash);
values._explicitQueryParams = true;
return Object.create({
isQueryParams: true,
values,
});
}
export default helper(explicitQueryParams);
import EmberRouter from '@ember/routing/router';
import config from './config/environment';
const Router = EmberRouter.extend({
location: 'none',
rootURL: config.rootURL,
_hydrateUnsuppliedQueryParams(state, queryParams) {
if (queryParams._explicitQueryParams) {
delete queryParams._explicitQueryParams;
return queryParams;
}
return this._super(state, queryParams);
}
});
Router.map(function() {
this.route('test');
this.route('redirect-route');
});
export default Router;
import Ember from 'ember';
import { explicitQueryParams } from '../helpers/explicit-query-params';
export default Ember.Route.extend({
beforeModel(transition){
transition.abort();
this.transitionTo('test', {queryParams: { _explicitQueryParams: true, foo: 'redirectFoo' }});
}
});
<h2> Click these links in order and watch the url</h2>
{{#link-to 'test' (explicit-query-params foo="foo" bar="bar")}} Explicit query params with foo and bar {{/link-to}}
<br>
{{#link-to 'test' (explicit-query-params foo="fooAlone")}} explicit query params with just foo will remove bar{{/link-to}}
<br>
{{#link-to 'test' (explicit-query-params foo="foo" bar="bar")}} Explicit query params with foo and bar to restore bar {{/link-to}}
<br>
{{#link-to 'test' (query-params foo="regularFoo")}} Regular query params with foo will continue to have bar {{/link-to}}
<br>
{{#link-to 'redirect-route'}} This route will abort its transition and redirect with 'explicit' params and remove bar {{/link-to}}
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment