Created
June 16, 2012 12:39
-
-
Save okeen/2941241 to your computer and use it in GitHub Desktop.
Simple dynamic flash messages for JSON based apps
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
$flash_width: 400px; | |
$distance_from_relative_anchor: 65px; | |
$left_offset: 0 - $flash_width / 2; | |
.flash_container { | |
position: absolute; | |
top: $distance_from_relative_anchor; | |
left: 50%; | |
width: $flash_width; | |
margin: {left: $left_offset;} | |
color: white; | |
div { | |
min-height: 20px; | |
border-radius: 5px; | |
padding: 5px; | |
} | |
.flash_notice { | |
background-color: green; | |
} | |
.flash_error { | |
background-color: red; | |
} | |
} |
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
class DynamicFlash extends Backbone.View | |
events: {} | |
messages: [] | |
initialize: (opts = {}) -> | |
@el= opts.el || $ "#flash_container" | |
@el.addClass "flash_container" | |
$(document.body).on "ajax:complete", "form", @showFlashMessage | |
$(document.body).on "ajax:error", "form", @showFlashMessage | |
super | |
render: () -> | |
showFlashMessage: (e, xhr, response) => | |
respObj = $.parseJSON xhr.responseText | |
message = respObj.message | |
#we are assuming a response object with 'status', 'message' and 'errors' keys | |
if (respObj.status == "created" or respObj.status == "ok") | |
$(@el).html "<div class='flash_notice'>#{ message || "OK" }</div>" | |
else if respObj.errors | |
$(@el).html "<div class='flash_error'>#{ message || "Error" }</div>" | |
$(@el).fadeIn(120) | |
setTimeout ()=> | |
$(@el).fadeOut(500) | |
, 5000 | |
(exports ? this).DynamicFlash = DynamicFlash |
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
format.json { render json: { model: @project, | |
status: 'created', | |
message: t("messages.projects.create") } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment