Ember, mixin to detect click outside of view/component

后端 未结 5 975
梦谈多话
梦谈多话 2021-01-06 05:48

I\'m writing a Mixin to handle when user clicks outside of a view/component.

This is the mixin:

App.ClickElsewhereMixin = Ember.Mixin.create({

  onC         


        
5条回答
  •  隐瞒了意图╮
    2021-01-06 06:17

    The ember way of doing it is Ember.run.bind. This takes care of binding and the run loop.

    App.ClickElsewhereMixin = Ember.Mixin.create({
    
      onClickElsewhere: Ember.K,
    
      setupListener: Ember.on('didRender', function() {
        this.set('clickHandler', Ember.run.bind(this, this.onClickElsewhere));
        Ember.$(document).click(this.get('clickHandler'));
      }),
    
      removeListener: Ember.on('willDestroyElement', function() {
        Ember.$(document).off('click', this.get('clickHandler'));
      }),
    });
    

提交回复
热议问题