Skip to content

Instantly share code, notes, and snippets.

@c-emil
Last active July 3, 2017 09:47
Show Gist options
  • Save c-emil/40a803a6866380468951f306c6108131 to your computer and use it in GitHub Desktop.
Save c-emil/40a803a6866380468951f306c6108131 to your computer and use it in GitHub Desktop.
History 2
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
Ember.Route.reopen({
historyOptions: {
isPrimaryRoute: false,
shouldSaveInHistory: true,
goBackRoute: null
},
setHistoryOptions(historyOptions) {
this.set('historyOptions', {
isPrimaryRoute: false,
shouldSaveInHistory: true,
goBackRoute: null
});
for (var historyOptionKey in historyOptions) {
this.set(`historyOptions.${historyOptionKey}`, historyOptions[historyOptionKey]);
}
},
actions: {
}
/**setupHistoryOptions: Ember.on('init', function () {
this.set('historyOptions', {
isPrimaryRoute: false,
shouldSaveInHistory: true,
goBackRoute: null
});
})*/
});
const HistoryMixin = Ember.Mixin.create({
getCurrentRouteName() {
return this.controllerFor('application').get('currentRouteName');
},
actions: {
willTransition() {
const theRoute = Ember
.getOwner(this)
.lookup(`route:${this.getCurrentRouteName()}`);
const isPrimaryRoute = theRoute.get('historyOptions.isPrimaryRoute');
const shouldSaveInHistory = theRoute.get('historyOptions.shouldSaveInHistory');
console.log('SETTINGS (willTransition): \n\nisPrimaryRoute: ' + isPrimaryRoute + '\n' + 'shouldSaveInHistory: ' + shouldSaveInHistory);
},
didTransition() {
Ember.run.next(this, function() {
const theRoute = Ember
.getOwner(this)
.lookup(`route:${this.getCurrentRouteName()}`);
const isPrimaryRoute = theRoute.get('historyOptions.isPrimaryRoute');
const shouldSaveInHistory = theRoute.get('historyOptions.shouldSaveInHistory');
console.log('SETTINGS (didTransition): \n\nisPrimaryRoute: ' + isPrimaryRoute + '\n' + 'shouldSaveInHistory: ' + shouldSaveInHistory);
});
}
}
});
export default HistoryMixin;
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('first');
alert('init 1');
this.route('second');
alert('init 2');
});
export default Router;
import Ember from 'ember';
import HistoryMixin from '../mixins/history-mixin';
export default Ember.Route.extend(HistoryMixin);
import Ember from 'ember';
import HistoryMixin from '../mixins/history-mixin';
export default Ember.Route.extend({
setupHistoryOptions: Ember.on('init', function setHistoryOptions() {
alert('init 1 - from');
this.setHistoryOptions({isPrimaryRoute: true});
})
});
import Ember from 'ember';
import HistoryMixin from '../mixins/history-mixin';
export default Ember.Route.extend({
setupHistoryOptions: Ember.on('init', function setHistoryOptions() {
alert('init 2 - from');
this.setHistoryOptions({shouldSaveInHistory: false});
})
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{#link-to 'first'}}
Go to first
{{/link-to}}
<hr>
{{#link-to 'second'}}
Go to second
{{/link-to}}
<hr>
<hr>
{{outlet}}
<br>
<br>
<h1>You're on a first (primary) page that sets `isPrimaryRoute` to `true`</h1>
<h1>You're on the second (not primary) page that sets `shouldSaveInHistory` to `false`</h1>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment