调用自定义 jquery 插件中的方法

发布于 2024-12-10 11:10:33 字数 1901 浏览 0 评论 0原文

我尝试构建我的第一个插件。我已经走到这一步了:

(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 方法。我怎样才能做到这一点?

I try to build my first plugin. That far I came yet:

(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 );

Now I need to call the run method from within the init method. How do i achieve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2024-12-17 11:10:33

啊抱歉伙计们 - 我现在找到了答案。看:

methods.run.call( this );

Ah sorry dudes - i found the answer now. Look:

methods.run.call( this );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文