Hi all, thanks for all your feedback.

I've tried to implement 2 suggestions that you have made here...

First one: observe the OBU event ONLY when changes have been made in
the form (actually it's on any event of the form, really, don't need
too much sophistication here :) ), and then stopObserving it when the
form is submitted:

<script type="text/javascript">
    var FormWatch2 = Class.create();
    FormWatch2.prototype = {
        initialize : function(form, options) {
            this.changed = false;
            this.form = $(form);
            // Observe any form event...
            this.form_event_observer = new Form.EventObserver(form,
this.setChanged.bind(this) );
            // Observe submit event...
 
Event.observe(this.form,'submit',this.unsetChanged.bind(this));
        },
        unsetChanged : function() {
            Event.stopObserving(window, 'beforeunload',
this.confirmExit, false);
        },
        confirmExit : function(ev) {
            return Event.stop(ev);
        },
        setChanged : function () {
            Event.observe(window, 'beforeunload', this.confirmExit);
        }
    }
</script>

That thing works only on firefox. Not only that, if I do:

ev.returnValue = "You haven't saved your changes !!"

right before return Event.stop(ev);  the script behaves exactly the
way I want :)


I've also tried a third version, which is the same as above, but
trying to observe every form element independently, replacing the "new
Form.EventObserver..." by this:

    Form.getElements(this.form).each(function(element) {
        Event.observe(element, 'blur', this.setChanged.bind(this),
false);
    });

But that doesn't work at all; I get this JS error (in Firefox, in IE
it's even more cryptic):

this.setChanged has no properties

And I'm not able to fix it.


As for the discussion of what would be the best form watcher, I agree
that comparing the elements to see if they have really changed and
things like that is very nice, but I would be really really happy if I
could this get working with ANY event; at least in IE6 / Firefox 2 /
Opera 9 which are the browsers that I'll be using this afterwards :)

Just to monitor something like "have you interacted with the form in
any way? show warning if you don't submit", fancy stuff like
serializing / comparing and all that stuff sound really nice, but I
lack the prototypejs skills to implement it yet :)

Thanks again for your help, I feel I'm getting closer to the
solution :)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to