Skip to content

Instantly share code, notes, and snippets.

@kennethdavidbuck
Last active December 26, 2015 02:09
Show Gist options
  • Save kennethdavidbuck/7075898 to your computer and use it in GitHub Desktop.
Save kennethdavidbuck/7075898 to your computer and use it in GitHub Desktop.
An attempt to return to the last route visited within a given resouce.
/**
*
*/
Em.RememberRouteMixin = Em.Mixin.create({
/**
* Whether or not this route should catch
* bubbling transitions and use them as a default
* transition object.
*/
catchTransition:false,
/**
* Default route path to when there is no
* lastTransition object
*/
defaultRoute:null,
/**
* Transition object to use in order to
* return to the last route visited for the
* current resource
*/
lastTransition:null,
/**
*
*/
redirect:function(){
"use strict";
if(this.get('catchTransition')) {
var lastTransition = this.get('lastTransition'),
defaultRoute = this.get('defaultRoute');
if(!Em.isEmpty(lastTransition)) {
lastTransition.retry();
} else if(!Em.isEmpty(defaultRoute)) {
this.transitionTo(defaultRoute);
}
}
},
/**
* Wait a moment, then attempt to bubble transition to
* parent an expectant parent route.
*/
afterModel:function(model,transition){
"use strict";
this._super();
if(!this.get('catchTransition')) {
Em.run.next(this,function(){
this.send('setLastTransition',transition);
});
}
},
/**
*
*/
actions:{
/**
*
*/
setLastTransition:function(transition){
"use strict";
if(this.get('catchTransition')) {
this.set('lastTransition',transition);
return false;
}
return true;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment