5

I'm working on a plugin using the ui widget factory. With a basic JQuery plugin, I can access the selector used by doing something like this...

$.fn.pluginname = function(){
    var thisWorks = this.selector;
};

However, when I use the widget factory, the selected element is accessed through the 'this.element' property, and 'this.element.selector' does not work the same way as in my first example. Anyone have a nice way around this? I'm looking through the widget factory source code on GitHub, but I haven't been able to find anything yet.

3 Answers 3

5
+50

The trick is that after the widget factory has completed creating your widget, it is essentially just a normal plugin afterwards. That means you can further extend it.

The idea is to save the original function, replace it with a custom function and make it pass the selector through. This code can be anywhere after the widget factory and before you use your object the first time. Just put it in your plugin file after calling $.widget().

The name of the widget in this example is test.

// Save the original factory function in a variable
var testFactory = $.fn.test;

// Replace the function with our own implementation
$.fn.test = function(obj){
    // Add the selector property to the options
    $.extend(obj, { selector: this.selector });

    // Run the original factory function with proper context and arguments
    return testFactory.apply(this,arguments);
};

This makes sure that this.selector is passed through as a named option. Now you can access it anywhere within your factory by referencing this.options.selector.

As an added plus, we didn't need to hack any jQuery UI code.

Sign up to request clarification or add additional context in comments.

3 Comments

Fantastic. Never thought of doing it that way. I need to play around with the arguments for what I'm doing, but it seems like it will work just fine. Thanks!
How would I do this if I'm just modifying (duckpunching) one of the widgets methods, for instance the tooltip's close method and need to access the original selector. Is this possible? Here's a non-working example: jsfiddle.net/corydorning/9grH5
Was able to figure it out using the principles above, although i had to overwrite the options of the prototype for the widget i was patching. Gist is here: gist.github.com/4445666
1

Just my couple cents... here is working JSFiddle example based on DarthJDG's answer... Probably someone will need it.

$(function () {
    $.widget("test.testwidget", {
        _create: function () {
            this.element.text('Selector: ' + this.options.selector)
        },
    });
}(jQuery));

// Save the original factory function in a variable
var testFactory = $.fn.testwidget;

// Replace the function with our own implementation
$.fn.testwidget = function(obj){
    // Add the selector property to the options
    $.extend(obj, { selector: this.selector });

    // Run the original factory function with proper context and arguments
    return testFactory.apply(this,arguments);
};

$(document).ready(function() {
    $('#test').testwidget({});
    $('.test').testwidget({});
});

Comments

-1

It is also possible to simply use

this.selector =  '#'+this.element.attr('id');

usually in the _create method. Of course this requires each instance of your widget to have a unique id which it usually should anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.