Skip to content

Instantly share code, notes, and snippets.

@ghempton
Created March 5, 2014 02:57
Show Gist options
  • Save ghempton/9360340 to your computer and use it in GitHub Desktop.
Save ghempton/9360340 to your computer and use it in GitHub Desktop.
Example of using EPF child sessions at the route level
`import CurrentUserMixin from 'outreach/core/current_user_mixin'`
class Route extends Em.Route with CurrentUserMixin
beforeModel: (transition) ->
parentRoute = if transition.state.handlerInfos.length > 1
transition.state.handlerInfos[transition.state.handlerInfos.length - 2].handler
else
null
@setupSession(parentRoute)
setupController: (controller, context) ->
controller.session = @session
context = @setupContext(context)
if context && context instanceof Ep.Model
context = @setupModel(context)
@currentModel = context
controller.content = context if context
setupContext: (context) -> context
# Similar to `model` but is called consistently
# inside of `setupController`
setupModel: (model) ->
@session.add(model)
setupSession: (parentRoute) ->
# Can be overriden
`export default Route`
`import Route from 'outreach/core/route'`
# Route which manages a transaction on it's content.
# E.g. when the route is exited, the transaction will
# be rolled back
class TransactionalRoute extends Route
setupSession: (parentRoute) ->
@session = parentRoute.session.newSession()
exit: ->
super()
# TODO destroy the session
actions:
willTransition: (transition) ->
isDirty = @session.isDirty
if isDirty && !transition.data.confirmed
transition.abort()
@dialogManager.confirm
title: "Cancel Edit"
message: "Are you sure you want to leave this page? There are unsaved changes."
confirmHandler: ->
transition.data.confirmed = true
transition.retry()
`export default TransactionalRoute`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment