Topic: Change coffeescript variable with onclick

Hi,
Been working on this all weekend and need some help.  Anyone know why this not working?

Coffee:
        confirmExit = ->
              "You have unsaved changes!"  if needToConfirm
        needToConfirm = true
        window.onbeforeunload = confirmExit
View:
<%= f.submit 'Save', class: "btn btn-large btn-primary", onclick: "needToConfirm = false" %></p>

I want to set needToConfirm to false if user clicks save button...

Last edited by dcastellano1 (2013-03-03 20:03:57)

Re: Change coffeescript variable with onclick

Hello
Try this code in your js.coffee file (take care of indents !)

jQuery ->
    
    $("#mydiv").on "click", ":submit", (event) ->
        $('input#needToConfirm').val(true)
        alert "You have unsaved changes !"

Hope this helps
Cheers

By the way I found this site can be helpful  http://js2coffee.org

Re: Change coffeescript variable with onclick

Yep
I guess 'event' returns the object clicked (in this case the submit button) and all its properties
Try alert event.name or something similar

Is your input needToConfirm correctly set to 'true' ?

Are you sure the action is fired? do a basic alert 'hello' to ensure :

$("#xyz").on "click", ":submit", (event) ->
    alert "Hello"

I don't understand why you have an onclick listener in your submit button...

Your js.coffee file (with the code I gave you) listen for a click event on the submit button, when a user click on it button , it fires the corresponding action/event in the js.coffee file.

I probably misunderstand what you're doing, and read you would be easier if you used BBCode for your code !

Cheers

Re: Change coffeescript variable with onclick

Have not used  BBcode - thanks i'll check it out.  Alert works so action is fired, but only if I remove ":session" from .on "click", - however then I  get nothing including no form submission

        needToConfirm = true   
        $("#question_save_changes").on "click", (event) ->    # Change global variable to FALSE if user clicks "Save" button.
            needToConfirm = false
            alert "Hello"       

        confirmExit = ->
              "You have unsaved changes!"  if needToConfirm

        window.onbeforeunload = confirmExit

<%= f.submit 'Save', class: "btn", id: "question_save_changes" %>

Re: Change coffeescript variable with onclick

Sorry

I really don't understand what you're doing... Where do you put your code?

What your html.erb looks like ? Where is located you coffee.js file ?

Please when you post some code put it between [ code ] [ / code ] (without spaces) it's the BBCode markup and make it easier to read

Cheers