Skip to content

Instantly share code, notes, and snippets.

@danevans
Last active August 24, 2018 01:56
Show Gist options
  • Save danevans/36d4d97dc6e753591ffa8867c43b8b68 to your computer and use it in GitHub Desktop.
Save danevans/36d4d97dc6e753591ffa8867c43b8b68 to your computer and use it in GitHub Desktop.
Query Param Refresh Model
import Ember from 'ember';
import { inject as service } from '@ember/service';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
router: service(),
actions: {
myAction(...args1) {
console.log('action', ...args1);
this.get('router').transitionTo('my-route', { queryParams: { currentUnitToken: '5678' } }).then((...args2) => {
console.log('then?', ...args2);
}).catch(error => {
console.log('error', error);
});
}
}
});
import EmberRouter from '@ember/routing/router';
import config from './config/environment';
const Router = EmberRouter.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('my-route');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
queryParams: {
currentUnitToken: {
refreshModel: true
}
},
currentUnitToken: null,
i: 0,
model() {
this.set('i', this.get('i') + 1);
console.log('calling model', this.get('i'));
return `test model ${this.get('i')}`;
}
});
<h1>Welcome to {{appName}}</h1>
<p>With the console open to observe the error</p>
<p>Click <button {{action "myAction"}}>Try This</button></p>
<p>You should see a transitionAbortedError</p>
<p>Subsequent clicks will work and clicking back and forth between the link with the different query param works.</p>
<p>Clicking back to home then trying again breaks but only using the button, not the link-to</p>
{{link-to "Home" "application"}}
{{link-to "My Route" "my-route" (query-params currentUnitToken="1234")}}
<br>
{{outlet}}
<br>
<br>
<p>query param value: {{currentUnitToken}}</p>
{
"version": "0.15.0",
"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.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment