How to extend plugin for jQuery?

I am writing a plugin for jQuery. I have a problem with an extension plugin. I wrote a plugin, for example: http://docs.jquery.com/Plugins/Authoring .

See the following code example:

(function($){
    var i18n    = {};
    var methods = {};
    $.fn.myPlugin = function(options){
        //...
   };
})(jQuery);

How can I extend a property i18n?

I want to be able to support internationalization plugin settings that are stored in a separate file. How should I do it?

+3
source share
4 answers

For instance:

// plugin definition
$.fn.hilight = function(options) {
  // Extend our default options with those provided.
  // Note that the first arg to extend is an empty object -
  // this is to keep from overriding our "defaults" object.
  var opts = $.extend({}, $.fn.hilight.defaults, options);
  // Our plugin implementation code goes here.
};
// plugin defaults - added as a property on our plugin function
$.fn.hilight.defaults = {
  foreground: 'red',
  background: 'yellow'
};

From here http://www.learningjquery.com/2007/10/a-plugin-development-pattern .

This is a very good tutorial to get you started.

+2
source

JQuery plugins usually extend the following parameters:

var i18nOpts = $.extend({}, i18n, options.i18n);

: http://docs.jquery.com/Plugins/Authoring#Defaults_and_Options

.

(function($){
    var i18n    = {};
    var methods = {};
    $.fn.myPlugin = function(options){
        var i18nOpts = $.extend({}, i18n, options.i18n);
   };
})(jQuery);

i18n , , .

$('#myDiv').myPlugin({
    i18n: {
        option1: "value",
        option2: "Value2"
    }
});
+1

The following is a handy template that I use for myself.

(function($){

var MyClass = function (element, options){

   this.options = $.extend({}, options);

   this.someFunction = function (){...}  //Public members

   var otherFunction = function(){...}   //Private members

   $(element).data('pluginInstance', this);   

}


$.fn.myPlugin = function(options){

    var myClassInstace = new MyClass(this, options);

    //Do other things with the object.
}

})(jQuery);
+1
source

You can do this with the $ method . extend (objectA, objectB) jQuery. I find it best to start learning jQuery plugin dev. from the main greeting of a world textbook such as this link http://www.tectual.com.au/posts/3/How-To-Make-jQuery-Plugin-jQuery-Plugin-Hello-World-.html or Check this post here fooobar.com/questions/1906242 / ... or

0
source

All Articles