Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery plugin call public function inside other public function

I defined my plugin base on http://docs.jquery.com/Plugins/Authoring

(function( $ ){

  var methods = {
    init : function( options ) {  },
    show : function( options ) {  },
    hide : function( ) {  },
    update : function( content ) { 
      // How to call the show method inside the update method
      // I tried these but it does not work
      // Error: not a function
      this.show(); 
      var arguments = { param: param };
      var method = 'show';
      // Error: options is undefined
      methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));  
    }
  };

  $.fn.tooltip = function( method ) {

    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    

  };

})( jQuery );

How to call the show method inside the update method?

EDIT:

The show method references this. Using methods.show(options) or methods['show'](Array.prototype.slice.call( arguments, 1 )); works to call the show method but then the reference to this seems to be wrong because I got a this.find(...) is not a function error.

The show method:

show: function(options) {
    alert("Options: " + options);
    alert("Options Type: " + options.interactionType);
    var typeCapitalized = capitalize(options.interactionType);
    var errorList = this.find('#report' + typeCapitalized);
    errorList.html('');
},
like image 580
Sydney Avatar asked Apr 13 '11 20:04

Sydney


1 Answers

var methods = {
  init : function( options ) {  },
  show : function( options ) {  },
  hide : function( ) {  },
  update : function( options ) { 

    methods.show.call(this, options);

  }
};
like image 142
Matt Ball Avatar answered Sep 28 '22 07:09

Matt Ball