2011-10-19 11 views
0

私は最初のプラグインを構築しようとしています。これまでのところ私はまだ来た:カスタムjqueryプラグインのメソッドを呼び出す

(function($){ 

    var settings = { 
    key1: 't1', 
    key2: 't2' 
    }; 

    var methods = { 
    init : function(options) { 

     return this.each(function() { 
     var $this = $(this); 

     console.log('init called'); 
     data = $this.data('snake'); 

     // If the plugin hasn't been initialized yet 
     if (!data) { 

      //Do setup stuff here 

      $this.data('snake', { 
      map: $this.find(".map"), 
      stats: $this.find(".stats") 
      }); 

      data = $this.data('snake'); 
     } 

     if (options) { 
      $.extend(settings, options); 
     } 

     // HERE I WOULD LIKE TO CALL THE RUN METHOD AND BE ABLE TO USE settings AND data VARIABLES 

     }); 

    }, 
    run : function() { 

     var $this = $(this), data = $this.data('snake'); 
     console.log('run called'); 
     //test for settings and data 
     console.log(settings); 
     console.log(data); 

    }, 
    test : function() { 

     return this.each(function() { 
     var $this = $(this), data = $this.data('snake'); 
     console.log('test called'); 
     //test for settings and data 
     console.log(settings); 
     console.log(data); 
     }); 

    }, 
    setup : function () { 
     console.log('setup called'); 
    }, 
    hide : function() {} 
    }; 

    $.fn.snake = function(method) { 

// console.log('call: ' + method); 

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

    }; 

})(jQuery); 

は今、私はinitメソッド内からrunメソッドを呼び出す必要があります。どのように私はそれを達成するのですか?

答えて

1

ああ、申し訳ありませんが、私は今答えを見つけました。見てください:

methods.run.call(this); 
+0

あなたはそれをどこで宣言しましたか? –

関連する問題