简体   繁体   English

jQuery $ .proxy和$ .on函数

[英]jquery $.proxy and $.on functions

I wanted to generalize my javascript plugin, the code (taken from bootstrap's tooltip plugin and changed a little bit): 我想概括一下我的javascript插件,代码(取自bootstrap的工具提示插件,并做了一些改动):

!function ($) {

   "use strict"; // jshint ;_;

   var Conversation = function (element, options) {
      this.init('conversation', element, options);
   };

   // PROTOTYPE DEFINITIONS

   Conversation.prototype = {

      constructor: Conversation,

      init: function (type, element, options) {

         this.type = type;
         this.$element = $(element);
         this.options = this.getOptions(options);
         var $that = $(this);


         this.$element.find('[data-toggle]').on('click', /*HERE*/);

      },

      getOptions: function (options) {
         return $.extend({}, $.fn[this.type].defaults, options, this.$element.data());
      },

      starConversation: function(e){

          console.log('starConversation called!');
      },

      selectConversation: function(e, arg){

         console.log('selectConversation called!');

      },

      selectAllConversations: function(e){
         console.log('selectConversation called!');
      }

   };


   $.fn.conversation = function ( option ) {
      return this.each(function () {
         var $this = $(this)
            , data = $this.data('conversation')
            , options = typeof option == 'object' && option;
         if (!data) $this.data('conversation', (data = new Conversation(this, options)));
         if (typeof option == 'string') data[option]()
      })
   };

   $.fn.conversation.Constructor = Conversation;

   $.fn.conversation.defaults = {
      selectAllButtonSel: '#selectAll'
   };


}(window.jQuery);

the problem is I wanted to generify like "all elements having data-toggle should call the function which is named the same as their data-toggle attribute". 问题是我想像“所有具有数据切换的元素都应调用与它们的数据切换属性相同的函数一样”来概括地说。

Normally, 一般,

this.$element.find('[data-toggle]').on('click', $.proxy(this['starConversation'], this));

works. 作品。 But I wonder whether I can reach clicked element's data-toggle attribute inside this['starConversation'] . 但是我不知道我是否可以在this['starConversation']到达被单击元素的data-toggle属性。 I tried also this: 我也试过这个:

$.proxy($that[$(this).data('toggle')], $that);

however $that variable somehow do not have starConversation function variable. 但是$that变量某种程度上没有starConversation函数变量。 Is scope is changed inside $.proxy function (or because of on() function)? 范围是否在$.proxy函数内更改(或由于on()函数)? I do not want to break the plugin pattern here. 我不想在这里破坏插件模式。 Is it possible? 可能吗?

I assume that when someone clicks on an element with a [data-toggle] attribute a function should be called whose name equals that of the value of [data-toggle] . 我假设当某人单击具有[data-toggle]属性的元素时,应调用一个函数,该函数的名称等于[data-toggle]的值。

Possible solution: 可能的解决方案:

this.$element.find('[data-toggle]').on('click', function(event) {
  var methodName = $(this).data('toggle');
  $that[methodName](event);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM