You could consider modifying your plugin to follow the jQuery UI
Widget format. This takes advantage of the widget factory, which
provides ways to access the methods and properties in 2 ways:

1) Through the element the plugin is applied to, eg:
    $("#myElement").smartList("selectedValue")

2) Or by getting an 'instance' of the widget, eg:
    var myList = $("#myElement").data("smartList");
    var val = myList.getSelectedValue();

If you don't want to go this route, then you need to return an
'instance object' instead of a jQuery object. This means your plugin
is not 'chainable' - ie, it must be the last method called on the
object.

To return an instance, create an object with pointers to your public
properties and methods...

return {
    options: options // property
,   getSelectedValue: getSelectedValue // method
,   insetItem: internalMethodName
}

There are a few ideas to get you started.

/Kevin


On Jan 4, 5:08 am, mehdi <mehdi.mous...@gmail.com> wrote:
> Hi,
> I've just developed a plugin that mimics the combo box control, albeit
> it's a special one. That's being defined as follows:
>
> (function($) {
>     $.fn.extend({
>         smartList: function(settings) {
>             //prepare settings, blah blah blah...
>
>             return this.each(function() {
>                 //whatever code goes here...
>             });
>         }
>     });
>
> })(jQuery);
>
> You know, to use this plugin, I could easily write the following
> JavaScript code:
>
> var $myList = $('myElement').smartList();
>
> No problem so far. Now, I need to define and access some functions
> that's specific to the smartList. say, e.g., getSelectedValue,
> insertItem, and the like. The problem is that I've got no idea how to
> address such a thing in JavaScript. i.e., I need to write things like:
>
> $myList.getSelectedValue();
> $myList.insetItem('foo', 'bar');
>
> But this isn't possible, since the $myList variable is a jQuery
> object.
>
> So I just defined some functions, say, $.smartList.getSelectedValue
> and the like... but in this approach, I've to pass the jQuery object
> to this functions as a mandatory parameter and this really sucks.
> i.e., I need to get the selected value of $myList this way:
>
> var value = $.smartList.getSelectedValue($myList);
>
> Is there any better approach to address such a thing?
>
> Any help would be highly appreciated,
>
> TIA,
> Mehdi

Reply via email to