reach the method outside of the plugin

316 views Asked by At

I have a boilerplate something like this,and wanna reach the some function inside of this plugin.

 ;
    (function ($, window, document, undefined) {

        'use strict';

        var old = $.fn.twbsPagination;

        // PROTOTYPE AND CONSTRUCTOR

        var TwbsPagination = function (element, options) {
            this.$element = $(element);
            this.options = $.extend({}, $.fn.twbsPagination.defaults, options);

            return this;
        };

        TwbsPagination.prototype = {

            constructor: TwbsPagination,

            destroy: function () {

                return this;
            },

            show: function (page) {

                return this;
            },


            getoptions: function () {
                console.log("run thıs function");
                //return options;
            }

        };

        // PLUGIN DEFINITION

        $.fn.twbsPagination = function (option) {
            var args = Array.prototype.slice.call(arguments, 1);
            var methodReturn;

            var $this = $(this);
            var data = $this.data('twbs-pagination');
            var options = typeof option === 'object' && option;

            if (!data) $this.data('twbs-pagination', (data = new TwbsPagination(this, options)));
            if (typeof option === 'string') methodReturn = data[option].apply(data, args);

            return (methodReturn === undefined) ? $this : methodReturn;
        };

        $.fn.twbsPagination.defaults = {

        };

        $.fn.twbsPagination.Constructor = TwbsPagination;

        $.fn.twbsPagination.noConflict = function () {
            $.fn.twbsPagination = old;
            return this;
        };

    })(jQuery, window, document);

my question how can I reach the get option function outside the plugin or how to modify plugin to do this . something like this.

var pager = $('#pagination-demo').twbsPagination({
totalPages: 35,
visiblePages: 7

    });
pager.prototype.getoptions());
1

There are 1 answers

4
Slico On BEST ANSWER

You have to write this:

$.fn.getoptions = function() {       
    console.log("run this function");
};

Below this:

 TwbsPagination.prototype = {
        constructor: TwbsPagination,

        destroy: function () {
            return this;
        },

        show: function (page) {
            return this;
        },
    };

    // HERE goes the function
    $.fn.getoptions = function(option) {       
        console.log("run this function");
    };

Q: but how can I reach the options and return options from $.fn.getoptions,when I try this it says me undefined

// plugin
 $.fn.getoptions = function(option) {       
        var settings = $.extend({
            // default
            width: "400px"
        }, option );
        console.log("width is: " + settings.width);
    };

You can access it like this:

pager.getoptions({
   width: 100
});

Will return "width is: 100"