Skip to content

Instantly share code, notes, and snippets.

@aaronvb
Last active December 18, 2015 16:19
Show Gist options
  • Save aaronvb/5810140 to your computer and use it in GitHub Desktop.
Save aaronvb/5810140 to your computer and use it in GitHub Desktop.
<div class="container">
{{#if notification}}
<div {{bindAttr class=":alert notification.type"}} id="notification">
<button type="button" class="btn-close" {{action "closeNotification"}}></button>
{{#if notification.title}}
<h3>{{notification.title}}</h3>
{{/if}}
{{notification.message}}
</div>
{{/if}}
{{outlet}}
</div>
App.ApplicationController = Ember.Controller.extend
# close notification alert
# bind to action in template, example: {{action "closeNotification"}}
# detects if persists and closes on next transition
#
closeNotification: ->
notification = @get('notification')
if notification
if notification.persists
console.log "Notification detected, clearing alert notification after next transition"
notification.persists = null
else
console.log "Notification detected, clearing alert notification now"
this.set('notification', null)
# notification alert
# type can be: error, info, success
# example: @get('controllers.application').notify({title: "Error!", message: "An error occurred in foobar.", type: "alert-error"})
#
notify: (options) ->
this.set('notification', options)
App.PostsNewController = Ember.ObjectController.extend
needs: ["application"] # allows access to application controller functions
# post save
#
save: ->
@set('model.title', @get('title'))
@set('model.description', @get('description'))
post = @get('model')
# if post is not valid, show flash message
#
post.on 'becameInvalid', this, (obj) ->
@get("controllers.application").notify({
title: "Error!",
message: "There was an error creating this post.",
type: "alert-error"})
# if post was created transition to posts index, show flash message and persist it to next transition
#
post.on 'didCreate', this, () ->
@get("controllers.application").notify({
title: "A new post!",
message: "#{@get('model.title')} was created.",
type: "alert-success",
persists: true})
@transitionToRoute('posts.index')
# commit post to server
#
@get('model.transaction').commit()
App.BeforeRoute = Ember.Route.extend
# close any open notifcations before a route loads
#
activate: ->
@controllerFor('application').closeNotification()
App.PostsIndexRoute = App.BeforeRoute.extend()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment