SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to override a function in a jQuery plugin and have access to options/variables

    I'm working on a plugin that needs to allow for some functions to be swapped out for custom ones. When I do the swap, the function runs, but it needs to have access to the options/variables that are inside the plugin. I've tried searching around, but I'm probably searching for the wrong thing. I'd appreciate help if anyone knows what I'm trying to do:

    Code:
    (function($){
        $.fn.test = function( options ){
    
            /* Set defaults */
            var defaults = {
                some_value: 'a',
                do_something: function(){
                    alert('this is the default function');
                }
            };
    
            /* Override defaults with options */
            var options = $.extend( defaults, options );
    
            $(this).click(function(){
                console.log( options.do_something() );
            });
        }
    })(this.jQuery);
    
    $('span').test({
        do_something: function(){
    
            // ReferenceError: options is not defined
            alert( options.some_value );
    
            // What I really want to do is set some_value
            options.some_value = 'b';
        }
    });
    jsfiddle: http://jsfiddle.net/BCwm4/

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The this keyword can be used to access the function, so you could use this.some_value for example.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I change line 36 to:

    Code:
    alert( this.some_value );
    The alert says "undefined".

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by skunkbad View Post
    If I change line 36 to:

    Code:
    alert( this.some_value );
    The alert says "undefined".
    When that very same change is made to your jsfiddle code, it works.

    See? http://jsfiddle.net/pmw57/BCwm4/2/
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was wrong. My code was slightly different because I was playing with it for so long, I had added some extra stuff. Thanks for your help. Everything is good now!
    Last edited by skunkbad; Oct 24, 2012 at 00:20. Reason: I was wrong


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
  •