Created
May 27, 2016 23:30
-
-
Save ZempTime/070ea9d857df70977e5682f1e8a26a80 to your computer and use it in GitHub Desktop.
RealtimeForm
This file contains hidden or 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 @RealtimeForm | |
constructor: (form) -> | |
@form = $(form) | |
@behavior = @form.data("realtime") | |
@monitorInputs() | |
monitorInputs: => | |
@radio_inputs = new RadioGroupInput @form.find(":radio"), @behavior | |
@inputs = $.map @form.find(":input").not(":input[type=submit]").not(":input[type=radio]"), (input, i) => | |
new TextyInput(input, @behavior) | |
class TextyInput | |
constructor: (input, behavior) -> | |
@input = $(input) | |
@behavior = if (@input.attr("realtime")) then @input.data("realtime") else behavior | |
@initial_value = @input.val() | |
@monitorInput() | |
monitorInput: => | |
@input.on "change", => | |
if @input.val() isnt @initial_value | |
if @behavior is "submit" | |
@input.parent("form").submit() | |
else if behavior is "savable" | |
$.each @form.find(":input[type=submit]"), (submitter) -> | |
submitter.show() | |
class RadioGroupInput | |
constructor: (radios, behavior) -> | |
@behavior = behavior | |
@radios = $(radios) | |
@initial_value = @radios.find(":checked").val() | |
@monitorInputs() | |
checked: => | |
@radios.find(":checked").val() | |
monitorInputs: => | |
window.radios = @radios | |
@radios.on "change", => | |
if @radios.find(":checked").val() isnt @initial_value | |
if @behavior is "submit" | |
@radios.parent("form").submit() | |
else if behavior is "savable" | |
$.each @form.find(":input[type=submit]"), (submitter) -> | |
submitter.show() | |
$(document).on "turbolinks:load", -> | |
new RealtimeForm $("[data-realtime]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment