coure2011
coure2011

Reputation: 42404

How to call a plugin method

Here is my code for the plugin

(function( $ ) {

    $.fn.myGrid = function(options) {

        var _myGrid = this;
        var _$myElement;
        var setOutTimer;
        var settings = $.extend({
            url: null,
            dataType: "json",
            colNames: []
        }, options );

        this.initiateGRID = function(that) {
            _$myElement = $(that);            
        }

        this.showInlineSuccess = function(msg){
            //Some Code
        }

        return this.each(function() {        
            _myGrid.initiateGRID(this);
        });

    };

}( jQuery ));

After the plugin is loaded I want to call the method showInlineSuccess from outside something like $.myGrid.showInlineSuccess('Hello');

How to achieve this?

Upvotes: 1

Views: 49

Answers (3)

schnill
schnill

Reputation: 955

You should design it like,

   (function( $ ) {

      function Grid(options) {

         this._myGrid = null,
         this._$myElement = null,
         // other properties of grid

      }

      // add methods to Grid class
      Grid.prototype = {

        initiateGRID : function(that) {
           var grid = this;     
           grid._$myElement = $(that);

        },

        howInlineSuccess : function(msg){
                //Some Code

        }
      }

      $.fn.myGrid = function(options) {

        var settings = $.extend({
                url: null,
                dataType: "json",
                colNames: []
            }, options );

        return new Grid(settings)

      }  

    }( jQuery ));

now you can call it like,

var grid = $('some selector').myGrid()
grid.showInlineSuccess('Hello')

if you want to call it like

var grid = $.myGrid()
grid.showInlineSuccess('Hello')

then change your plug in definition as,

$.myGrid = function(options) { // code

Upvotes: 1

james emanon
james emanon

Reputation: 11807

This gives you what you need, but don't believe this is the best approach.

$.fn.myGrid().showInlineSuccess('asdfasdfasdf');

note the (), after myGrid

Upvotes: 1

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

Use this:

$.fn.myGrid.showInlineSuccess('Hello');

instead of:

$.myGrid.showInlineSuccess('Hello');


You are missing fn

Upvotes: 0

Related Questions