admin管理员组

文章数量:1277885

Consider the following base code:

(function($) {
    $.fn.myPlugin = function(settings) {
        return this.each(function() {
            //whatever
        });
    };
});

The plugin returns a jQuery object. The question is how am I supposed to write a plugin that returns a custom object so that I can do something like this:

var api = $('div.myelement').myPlugin();
api.onMyEventName(function(e, whateverParam) {
    //whatever
});

It'd be highly appreciated if you could write some lines of code that describes me how to do that, how to call the onMyEventName function on a custom api object...

Thanks.

Consider the following base code:

(function($) {
    $.fn.myPlugin = function(settings) {
        return this.each(function() {
            //whatever
        });
    };
});

The plugin returns a jQuery object. The question is how am I supposed to write a plugin that returns a custom object so that I can do something like this:

var api = $('div.myelement').myPlugin();
api.onMyEventName(function(e, whateverParam) {
    //whatever
});

It'd be highly appreciated if you could write some lines of code that describes me how to do that, how to call the onMyEventName function on a custom api object...

Thanks.

Share Improve this question edited Apr 1, 2010 at 17:34 Crescent Fresh 117k27 gold badges157 silver badges140 bronze badges asked Apr 1, 2010 at 17:31 user307102user307102 231 silver badge3 bronze badges 2
  • 3 Why can't you just return your object? – Jeremy Commented Apr 1, 2010 at 17:34
  • It might brake the JQuery design contract, better to ask. – Vajk Hermecz Commented Feb 18, 2014 at 10:24
Add a ment  | 

2 Answers 2

Reset to default 14
(function($) {
    function MyApi($this, settings) {
        this.$this = $this;
        this.settings = settings;
    };

    MyApi.prototype.output = function (val) {
       // use this.$this to access the original jQuery object

       return this.settings[val];
    };

    $.fn.myPlugin = function(settings) {
        return new MyApi(this, settings);
    };
});

Note that we've passed this from $.fn.myPlugin() into the MyApi constructor; this allows you to access the jQuery object that myPlugin() was originally called on within MyApi methods.

You can also do the same using the object literal syntax:

(function($) {
    $.fn.myPlugin = function(settings) {
        return {
            settings: settings,
            output: function (val) {
                // use this.$this to access the original jQuery object

                return this.settings[val];
            },
            $this: this
        };
    };
});

Then;

var something = $('#something').myPlugin({
   val: 'Lemon'
});

alert(something.output('val')); // Lemon

... again, we've captured the value of this (the jQuery object) into a property $this on the newly constructed objected, to gain access to the original jQuery object.

There is a great article by Hector Virgen detailing a possible solution (also used in bootstrap) to adress this question.

The key moment is basically storing your API object in the data section of the node:

$.fn.myplugin = function() {
    return $.each(function() {
        ...
        myplugin = new MyPlugin(this)
        ...
        $(this).data('myplugin', myplugin);
    }
}

After this, users can easily access the object by:

$(this).data("myplugin")

A possibly useful extension to this solution could be defining a mypluginapi method as a shorthand, for accessing your API object:

$.fn.mypluginapi = function() {
    return $(this).myplugin().data('myplugin')
}

本文标签: