Skip to content

Instantly share code, notes, and snippets.

@thomaswrenn
Last active September 30, 2018 23:00
Show Gist options
  • Save thomaswrenn/f1e9c43effd0d9a603113749240da505 to your computer and use it in GitHub Desktop.
Save thomaswrenn/f1e9c43effd0d9a603113749240da505 to your computer and use it in GitHub Desktop.
Asynchronously Fetched Default Param Value Ember 2.12
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
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('parent', function() {
this.route('child');
});
this.route('sibling');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel() {
console.log('index');
this.replaceWith('parent');
}
});
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel() {
console.log('parent.child');
}
});
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel() {
console.log('parent.index');
},
redirect() {
console.log('redirecting parent.index to parent.child');
return this.replaceWith('parent.child');
}
});
import Ember from 'ember';
const FETCHED_VALUE = 'fetchedValue';
export default Ember.Route.extend({
queryParams: {
requiredParam: {
refreshModel: true
},
optionalParam: {
refreshModel: true
},
},
requiredParam: 'defaultParentValue',
optionalParam: 'defaultParentValue',
beforeModel(transition) {
console.log('parent');
const targetName = this.router.router.activeTransition.targetName;
console.log('On way to', targetName);
const currentQueryParams = transition.queryParams;
const { requiredParam } = currentQueryParams;
if (!requiredParam) {
console.log('didn\'t have requiredParam, aborting and doing async to fetch');
this.router.router.activeTransition.abort();
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.run.later(() => resolve(FETCHED_VALUE), 30);
})
.then((asynchronouslyFetchedValue) => {
console.log('resolved fetch promise');
const requiredParam = asynchronouslyFetchedValue;
const queryParams = Object.assign({}, currentQueryParams, {
requiredParam,
});
console.log('Transitioning to', queryParams);
this.replaceWith(targetName, { queryParams });
});
} else {
console.log('had requiredParam', requiredParam);
}
}
});
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel() {
console.log('parent.sibling');
}
});
import Ember from 'ember';
export default Ember.Route.extend({
queryParams: {
requiredParam: {
refreshModel: true
},
optionalParam: {
refreshModel: true
},
},
requiredParam: 'defaultUncleValue',
optionalParam: 'defaultUncleValue',
beforeModel() {
console.log('uncle');
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{
"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": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment