Skip to content

Instantly share code, notes, and snippets.

@c-emil
Last active July 1, 2017 11:51
Show Gist options
  • Save c-emil/a3baa597179ade3133bf1640a3ab1fcd to your computer and use it in GitHub Desktop.
Save c-emil/a3baa597179ade3133bf1640a3ab1fcd to your computer and use it in GitHub Desktop.
History computed
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
Ember.Route.reopen({
historyOptionsComputed: Ember.computed('historyOptions', function historyOptionsComputed() {
const isPrimaryRoute = this.get('historyOptions.isPrimaryRoute');
const shouldSaveInHistory = this.get('historyOptions.shouldSaveInHistory');
alert('historyOptions updated: ' + isPrimaryRoute + shouldSaveInHistory);
const historyOptions = {
isPrimaryRoute: false,
shouldSaveInHistory: true,
goBackRoute: null
};
for (var historyOptionKey in this.get('historyOptions')) {
historyOptions[historyOptionKey] = this.get(`historyOptions${historyOptionKey}`);
}
return historyOptions;
})
});
const HistoryMixin = Ember.Mixin.create({
actions: {
willTransition() {
const isPrimaryRoute = this.get('historyOptionsComputed.isPrimaryRoute');
const shouldSaveInHistory = this.get('historyOptionsComputed.shouldSaveInHistory');
alert('SETTINGS (willTransition): \n\nisPrimaryRoute: ' + isPrimaryRoute + '\n' + 'shouldSaveInHistory: ' + shouldSaveInHistory);
},
didTransition() {
const isPrimaryRoute = this.get('historyOptionsComputed.isPrimaryRoute');
const shouldSaveInHistory = this.get('historyOptionsComputed.shouldSaveInHistory');
alert('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');
this.route('second');
});
export default Router;
import Ember from 'ember';
import HistoryMixin from '../mixins/history-mixin';
export default Ember.Route.extend(HistoryMixin);
import Ember from 'ember';
export default Ember.Route.extend({
setHistoryOptions: Ember.on('activate', function setHistoryOptions() {
this.set('historyOptions', {isPrimaryRoute: true});
})
});
import Ember from 'ember';
export default Ember.Route.extend({
setHistoryOptions: Ember.on('activate', function setHistoryOptions() {
this.set('historyOptions', {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