3

jQueryのプラグインを書いています。拡張プラグインに問題があります。たとえば、プラグインを作成しました: http://docs.jquery.com/Plugins/Authoring

次のコード例を参照してください。

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

プロパティを拡張するにはどうすればよいi18nですか?

別ファイルに保存されている国際化プラグインの設定に対応したい。どうすればいいですか?

4

4 に答える 4

2

例えば:

// 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'
};

ここからhttp://www.learningjquery.com/2007/10/a-plugin-development-pattern

これは、始めるのにとても良いチュートリアルです

于 2012-05-01T16:37:55.787 に答える
1

jQueryプラグインは通常、次のようなオプションを拡張します。

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"
    }
});
于 2012-05-01T16:32:23.500 に答える
1

以下は、私が自分で使用する便利なテンプレートです..

(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);
于 2012-05-01T17:26:21.813 に答える
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

于 2012-07-23T11:46:57.807 に答える