views:

32

answers:

1

Here is my plugin

(function($){
    $.fn.myPlugin = function(options){

        var defaults = {
            width: 800
        };

        var defaults = $.extend(defaults, options);

        var self = this;

        function init(obj){
            /*Initialize object*/
            self.myPlugin.doAnimation(600,400);
        }

        $.fn.myPlugin.doAnimation = function(lV, rV){
            /*Doing some animation work*/
        }           

        return this.each(function(options){
            init(this);
        });         
    }
})(jQuery);

I am trying like this

var t = $('#id1').myPlugin();
t.doAnimation();  //getting error here, t.doAnimation is not a function
A: 

You can't. You're not returning the plugin. You're returning the jQuery object with the matched element(s) (like you probably should). The testing function is private to the myPlugin function anyway.

To call a method against a jQuery object, you would need to extend jQuery like you did for your myPlugin(), as in:

function($){
    $.fn.myPlugin = function(options) {
        ...
    }
    $.fn.testing = function(options) {
        ...
    }
})(jQuery);

Of course this would be completely separate from the original plugin.

I don't know what your plugin does, but if you need to share some data between plugins on a per-element basis, you could probably use jQuery's .data() method.

patrick dw
No, i have to perform an action. This action may required anytime. what shld i do?
coure06
@coure06 - Could you briefly describe the action? What are the requirements that it needs to be part of `myPlugin`?
patrick dw
@patrick dw: I might be wrong, but I believe he wants a method in his plugin's namespace, so he probably wants $.fn.myPlugin.testing = function(...){...}. That wouldn't clutter jQuery's namespace while making the method accessible from the outside.
Thomas
@Thomas - Could be. I'm not entirely sure what OP is after.
patrick dw
I am animating some content. That animation is based on some values to be passed to that method.
coure06
That animation is of different type, Clicking on button01 will pass x,y value to animate method of plugin, clicking on button02 will pass value a,b and so on...
coure06
@coure06 - If the animation is based on what is passed, couldn't you just define what is being passed as an option, then have your plugin perform the proper animation based on that option?
patrick dw
Checkout my updated code
coure06