Last active
December 18, 2015 16:19
-
-
Save aaronvb/5810140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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