JQuery UI - Using Widgets Polymorphically

I was planning to create multiple widgets using JQuery UI and use them polymorphically.

So, let's say I have two widgets named widget_1 and widget_2, both of which define a "public" method called testMethod (). I would like to blindly take any object and call testMethod. Sort of:

widget.testMethod()

      

However, JQuery seems to require me to know which object I have, since the method call syntax uses the form:

$("#widget_1").widget_1("testMethod"); 

      

and

$("#widget_2").widget_2("testMethod"); 

      

Are there any other parameters that I should be using for displaying widgets?

Thanks.

+2
jquery jquery-ui


a source to share


1 answer


Not recommended, but you can do it this way:

$.fn.testMethod = function(param1, param2){
   var id = this.attr("id");  //'this' references the selected element(s)
   return this;
};

      

Then you could do:



$("#widget_1").testMethod('foo', 'bar');

      

But you would pollute the $ "namespace

0


a source to share







All Articles