Created
September 16, 2011 17:45
-
-
Save Nakort/1222669 to your computer and use it in GitHub Desktop.
My First CoffeeScript
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
String.prototype.capitalize = -> | |
return @charAt(0).toUpperCase() + @slice(1) | |
window.remove_fields = (link) -> | |
($ link).prev("input[type='hidden']").val 1 | |
($ link).parent.hide | |
window.add_fields = (link, association, content) -> | |
new_id = new Date().getTime | |
regexp = new RegExp("new_" + association, "g") | |
($ link).parent.after content.replace(regexp, new_id) | |
window.clearForm = (form) -> | |
($ ':input', form).each -> | |
type = @type | |
tag = @tagName.toLowerCase() | |
if (type=="hidden" || type == 'text' || type == 'password' || tag == 'textarea') | |
@value = "" | |
else if (type == 'checkbox' || type == 'radio') | |
@checked = false | |
else if (tag == 'select') | |
@selectedIndex = -1 | |
jQuery -> | |
$("form") | |
.bind "ajax:error", (event, xhr, status) -> | |
errors = JSON.parse xhr.responseText | |
($ "#error_explanation").remove | |
errors_container = ($ "<div>").attr "id","error_explanation" | |
list = ($ "<ul>") | |
number_of_errors = 0 | |
$.each errors, (k, v) -> | |
number_of_errors++ | |
list.append $("<li>").html(k.capitalize() + " " + v) | |
errors_container | |
.append($("<h2>").html(number_of_errors + " errors prohibited this resource from being saved")) | |
.append list | |
($ @).prepend errors_container | |
if ($ "#map").size() | |
google.load "maps", "3", {other_params:'sensor=false', callback: -> initialize_map("#map")} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment