views:

85

answers:

3

hi,

i wrote a little jQuery button plugin - it contains a method for applying the onclick-function - here's the code

(function ($) {
  $.fn.tButton = function () 
  {
    this.setFN = function(fn)
    {
     alert("function set.");
    }
  };

})(jQuery);

i'm using this code to initialize it (on a div):

var button = $("#myButton").tButton();

now the problem: when trying to apply the setFN function:

button.setFN(function(){dosomething();});

i'm getting an error: button.setFN is not a function

i already tried this.bind instead but didn't help. anyone knows what's wrong?

thx

A: 

The function is tButton. If should read like this:

var button = $("#myButton").tButton(function(){dosomething();});
Chris Love
where does setFN involve? .. :/
Fuxi
+2  A: 

You aren't returning anything from the tButton function so the value of tButton isn't what you think it is. Try returning this from tButton() so to get the jQuery object back out of it. Also, I don't think this is a good way to do it as you are basically extending jQuery in a non-standard way. A better way would be to have tButton take the callback function as an argument and apply it to just the matching elements. I would also use a different pattern for defining the plugin (similar to the UI plugins).

(function ($) {
  $.fn.extend( {
      tButton: function(callback) {
          return this.each( function() {
             new $.TButton(this,callback);
          });
      }
  });

  $.TButton = function(elem,callback) {
       $(elem).click(callback);
  };
})(jQuery);
tvanfosson
i tried adding return this to at the end of the tButton function - same error.i think you're right about jquery standards, but i need to be able assigning/changing the button function afterwards.
Fuxi
Then have the function take multiple arguments and do different things based on those arguments. If, for instance, the argument list is the string 'click' and a function, reapply the click function to the element, otherwise do the normal thing.
tvanfosson
thanks for your example - looks plain and simple :)there's only one problem - it won't debug in firebug .. dunno if it's my system.
Fuxi
`.extend` is unnecessary. `$.fn.tButton = function...` works fine as well.
Doug Neiner
Only because I'm only adding a single method. I use the same pattern regardless.
tvanfosson
A: 

Here is the pattern you can use do this:

$.TButton = function(el){
    var $el = $(el);
    $el.data('TButton', this); // Store a reference to the TButton object on the DOM element
    // Use this.functionName to make it "public"
    this.setFN = function(callback){
       // Namespace the click events so you don't accidently remove
       // other click events bound to the button.
       $el.unbind('click.tbutton').bind('click.tbutton', callback );
    }
}

$.fn.tButton = function(func){
   return this.each(function(){ // Don't break the chain
      if(! func){ // If no parameter is passed, this is a constructor
        (new $.TButton(this));
      } else { // If the parameter (expected to be a function), call setFN
         var button = $(this).data('TButton');
         if(button) button.setFN(func);
      }
   });          
}

Now you can initialize using this:

$("button").tButton();

And can call setFN two ways like this:

$("button").tButton(function(){ ... });
// or
$("button").data('TButton').setFN( function(){ ... } );
Doug Neiner