Last active
September 30, 2018 23:00
-
-
Save thomaswrenn/f1e9c43effd0d9a603113749240da505 to your computer and use it in GitHub Desktop.
Asynchronously Fetched Default Param Value Ember 2.12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
beforeModel() { | |
console.log('index'); | |
this.replaceWith('parent'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
beforeModel() { | |
console.log('parent.child'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
beforeModel() { | |
console.log('parent.sibling'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
queryParams: { | |
requiredParam: { | |
refreshModel: true | |
}, | |
optionalParam: { | |
refreshModel: true | |
}, | |
}, | |
requiredParam: 'defaultUncleValue', | |
optionalParam: 'defaultUncleValue', | |
beforeModel() { | |
console.log('uncle'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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