对 Jquery 的 Tigger 有点疑问

class Plugin {

  constructor(element, options) {
    this._setup(element, options);
    var pluginName = getPluginName(this);
    this.uuid = GetYoDigits(6, pluginName);

    if(!this.$element.attr(`data-${pluginName}`)){ this.$element.attr(`data-${pluginName}`, this.uuid); }
    if(!this.$element.data('zfPlugin')){ this.$element.data('zfPlugin', this); }
    /**
     * Fires when the plugin has initialized.
     * @event Plugin#init
     */
    this.$element.trigger(`init.zf.${pluginName}`);
  }

  destroy() {
    this._destroy();
    var pluginName = getPluginName(this);
    this.$element.removeAttr(`data-${pluginName}`).removeData('zfPlugin')
        /**
         * Fires when the plugin has been destroyed.
         * @event Plugin#destroyed
         */
        .trigger(`destroyed.zf.${pluginName}`);
    for(var prop in this){
      this[prop] = null;//clean up script to prep for garbage collection.
    }
  }
}

这段代码里面的

this.$element.trigger(`init.zf.${pluginName}`);

不是很懂,代码里面没有声明任何 "init.zf.xxx" 这类事件,那这个 trigger 触发的是什么?

阅读 1.9k
2 个回答

这个事件是提供给使用者使用的,不需要在这里声明,比如element.on(”xxx”,function(){})

The constructor should also call an _init() method, and if needed an _events() method, followed by a DOM event indicating that the plugin is done being initialized.

function Plugin(element, options) {
  // ...

  this._init();
  this._events();
  this.$element.trigger('init.zf.plugin');
}

看到官网的这个表述:followed by a DOM event indicating that the plugin is done being initialized,感觉很迷

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题