3

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

See the following example code:

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

How can I extend the property i18n?

I want to be able to support the internationalization plug-in settings which are stored in the separate file. How should I do?

4 Answers 4

2

For example:

// 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 nice tutorial to get started with

Sign up to request clarification or add additional context in comments.

Comments

1

jQuery plugins usually extend options like this:

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

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

This happens inside the plugin itself.

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

i18n only exists inside that function, to extend it, you can now pass options to the plugin.

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

7 Comments

@IgorTimoshenko: The i18n variable can only be accessed from inside the anonymous function it's inside.
@IgorTimoshenko: Do what? What are you trying to do? You can't access i18n from outside of that function. You need to pass options to the plugin when you call it, and then use $.extend like I showed to extend it (from inside the plugin).
I understand, but I don't know how to do it, so that these settings are stored in the separate file. I'll ask in another way: how best to organize support for internationalization?
@IgorTimoshenko: I think you should edit the question to describe your real question.
@IgorTimoshenko: Take a look at coder's answer. You can have your separate file assign values to $.fn.myPlugin.defaults.
|
1

Below is a handy template 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);

Comments

0

You can do it by $.extend( objectA, objectB ) method of jQuery. I reckon youl'd better start learning jquery plugin dev. from basic hello world tutorial 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 over here https://stackoverflow.com/a/11611732/1539647 or

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.