SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot w1nk5's Avatar
    Join Date
    Aug 2009
    Location
    Ontario, Canada
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Object Help

    Here is my object:

    Code:
    function formWatcher(form, options) {
    
        this.form = $(form); console.log(this.form);
        this.submitted = false;
        this.formcontents = this.form.serialize();
        this.options = {
            "formMessage" : "If you leave this page now, your changes will not be saved.",
            "ajaxMessage" : "This page is still processing your request.",
            "requireSubmit" : false,
            "exempt" : []
        };
    
        this.form.bind('submit', function() {
            submitted = true;
        });
    
        $.each(this.options["exempt"], function(e_index, e_value) {
            this.exemptElement(e_value);
        });
    
        $(window).unload(function(){
            this.confirmExit(this);
        });
    
        this.exemptElement = function(element) {
            if(element) {
                element.click(function() {
                    this.submitted = true;
                });
            }
        };
    
        this.confirmExit = function(ev) {
            this.newcontents = this.form.serialize();
    
            if (((this.formcontents != this.newcontents) || this.options.requireSubmit) && !(this.submitted)) {
                ev.stop();
                ev.returnValue = this.options.formMessage;
            } else {
                return true;
            }
        };
    };
    Now when I attempt to leave the window, I receive "TypeError: this.confirmExit is not a function".

    Please help!

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,276
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    "this" takes on a different value when you're inside a callback. You'll need to set a local variable to keep the value of your new formWatcher object.

    Code:
        var thisFormWatcher = this;
    
        $(window).unload(function(){
            thisFormWatcher.confirmExit(this);
        });
    "First make it work. Then make it better."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •