Last active
August 29, 2015 13:55
-
-
Save kalpesh-fulpagare/8704258 to your computer and use it in GitHub Desktop.
My common js methods
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
flashTimer = null | |
$(document).ready -> | |
c = undefined | |
CommonJs = | |
settings: | |
domInputs: -> | |
$("input[type='text'], input[type='email'], input[type='password']") | |
init: -> | |
c = @settings | |
@bindUIActions() | |
bindUIActions: -> | |
@setMaxLengthToInputs() | |
$(document).ajaxComplete -> | |
CommonJs.setMaxLengthToInputs() | |
hideFlash() | |
setMaxLengthToInputs: -> | |
c.domInputs().attr "maxlength", 255 | |
CommonJs.init() | |
root = exports ? this | |
root.toggleDoms = (show, hide) -> | |
show.show() | |
hide.hide() | |
# type => alert-success, alert-info, alert-error | |
root.displayFlash = (message, type) -> | |
flash = "<div class='alert fade in " + type + "'><button data-dismiss='alert' class='close'>×</button>" | |
flash += message + "</div>" | |
$("div.alert:first").remove() | |
$(MAIN_CONTAINER).prepend flash | |
$('html,body').animate({scrollTop: $(".alert:first").offset().top-50},'slow'); | |
hideFlash() | |
root.hideFlash = -> | |
clearTimeout flashTimer | |
flashTimer = setTimeout(-> | |
$(".alert:first").remove() unless $(".alert:first").hasClass("alert-danger") | |
, 6000) | |
root.truncate = (str, len) -> | |
if len is undefined | |
len = 20 | |
return str if str.length <= len | |
str.substring(0, len) + "..." | |
# displayErrors("#{j @user.errors.to_json.html_safe}", "user"); create.js.haml | |
root.displayErrors = (error_string, modelName) -> | |
error_hash = JSON.parse error_string | |
#error_hash = jQuery.parseJSON error_string | |
$(".errorSpan").remove() | |
error_dom = "" | |
$.each error_hash, (attr, error) -> | |
error_dom = "<span class='errorSpan'>#{error}</span>" | |
attr = attr.replace(".", "_attributes][") | |
el = $("[name^='#{modelName}[#{attr}]']") | |
if el.prop("type") is "radio" or el.prop("type") is "checkbox" | |
el.parents(".controls:first").append error_dom | |
else | |
$(error_dom).insertAfter el | |
$('html,body').animate({scrollTop: $(".errorSpan:visible").offset().top-100},'slow'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment