Modify options and use it in my jquery plugin methods

Modify options and use it in my jquery plugin methods

Hy all,

It's the first time i try to create a jquery plugin using the official Plugins/Authorizing .

Please see above a little example of my first test :

  1. (function( $ ){
  2.       var methods = {
  3.             init: function( settings ){
  4.                   // Treatments 
  5.                   alert( settings.my_setting1 );
  6.             },
  7.             function1: function( settings ){
  8.                   // Treatments 
  9.                   alert(  settings.my_setting2  );
  10.             }

  11.       }

  12.       $(this).change(function( settings ){
  13.             alert(  settings.my_setting2  );
  14.       }

  15.       $.fn.maxUPload = function(options, method){
                var settings = {
  16.                   my_setting1: 'value',
  17.                   my_setting2: 'value',
  18.                   my_group1: {
  19.                         my_group1_setting1: 'value',
  20.                         my_group1_setting2: 'value',
  21.                   }
                };
                return $(this).each(function() {
                    if ( options ) {
                        $.extend( settings, options );
                    }
                });

                // Method calling logic
                if ( methods[method] ) {
                  return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
                } else if ( typeof method === 'object' || ! method ) {
                  return methods.init.apply( this, arguments );
                } else {
                  $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
                }
            }

















  22. })( jQuery );
In this case, my functions cannot read my settings properties.


In order to avoid this problem, i have change the code by this :
  1. (function( $ ){
  2.      
  3.       var settings = {
  4.             my_setting1: 'value',
  5.             my_setting2: 'value',
  6.             my_group1: {
  7.                   my_group1_setting1: 'value',
  8.                   my_group1_setting2: 'value',
  9.             }
          };

  10.       var methods = {
  11.             init: function( settings ){
  12.                   // Treatments 
  13.                   alert( settings.my_setting1 );

  14.             },
  15.             function1: function( settings ){
  16.                   // Treatments 
  17.                   alert(  settings.my_setting2  );
  18.             }

  19.       }



  20.       $(this).change(function( settings ){
  21.             alert(  settings.my_setting2  );

  22.       }


  23.       $.fn.maxUPload = function(options, method){
                return $(this).each(function() {
                    if ( options ) {
                        $.extend( settings, options );
                    }
                });

                // Method calling logic
                if ( methods[method] ) {
                  return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
                } else if ( typeof method === 'object' || ! method ) {
                  return methods.init.apply( this, arguments );
                } else {
                  $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
                }
            }
















  24. })( jQuery );
In this case, all functions can be used my settings properties.


The problem is when i want to instanciated my plugin by this code :
  1. J('#pieces_jointes').maxUPload({
  2.       my_setting1: 'other_value',
  3.       my_setting2: 'other_value'
  4. });
Indeed, with my first plugin' code, settings properties take the new value passed BUT the settings cannot be used by my plugings methods.

On my second plugin' code, settings properties take the default value (they ignored the new values passed during instanciation) BUT settings can be used by all my plugin methods.

How can I modify my 2 codes in order to have the possibility to add new value to my settings properties and passed these properties to all my plugin methods ?

Thanks a lot for your response!