Last active
August 29, 2015 14:21
-
-
Save cristianferrarig/2731051589ab4d44a110 to your computer and use it in GitHub Desktop.
Forms changes detector
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
Form = {} | |
Form.serializedForms = {} | |
# | |
# USAGE: | |
# | |
# First serialize form | |
# Form.saveSerialized( $("#myform") ) | |
# | |
# events: | |
# "change select.form-control" : "detectChanges" | |
# "keyup input.form-control" : "detectChanges" | |
# | |
# detectChanges: (ev)=> | |
# Form.changeDetector(ev) | |
# | |
Form.changeDetector = (ev)=> | |
console.log "validate form" | |
form = $(ev.currentTarget).parents("form") | |
name = form.attr("data-form-name") | |
new_content = form.serialize() | |
initial_content = Prey.Helpers.Form.serializedForms[name] | |
# For some browsers, `attr` is undefined; for others, `attr` is false. Check for both. | |
if typeof initial_content isnt "undefined" and initial_content isnt false | |
if initial_content != new_content | |
Form.enableSubmit(form) | |
else | |
Form.disableSubmit(form) | |
else | |
alert "Debes serializar el formulario al iniciar la vista!" | |
Form.saveSerialized = (form)=> | |
console.log "serialize form" | |
form_name = form.attr("data-form-name") | |
content = form.serialize() | |
Form.serializedForms[form_name] = content | |
Form.disableSubmit(form) | |
Form.disableSubmit = (form)=> | |
console.log "Disable submit button" | |
button = form.find("button[type=submit]") | |
unless button.hasClass("btn-default") | |
button | |
.removeClass("btn-primary") | |
.addClass("btn-default") | |
.attr("disabled",true) | |
Form.enableSubmit = (form)=> | |
console.log "Enable submit button" | |
button = form.find("button[type=submit]") | |
unless button.hasClass("btn-primary") | |
button | |
.removeClass("btn-default") | |
.addClass("btn-primary") | |
.attr("disabled",false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment