Access jquery event from ember component action

ahmeij

I'm trying to work with a simple overlay component, and close this overlay if someone clicks outside of the overlay content:

<div class="overlay" {{action 'close' on='click'}}>
  <div class="item">
    <form {{action 'submit' on='submit'}}>
      {{yield}}
      {{#link-to closeRoute class="close"}}Close{{/link-to}}
    </form>
  </div>
</div>

The component looks like this:

import Ember from 'ember';

export default Ember.Component.extend({
  actions: {
    submit: function() {
      this.sendAction();
    },
    close: function(param) {
      console.log(param); // -> undefined
      console.log(this);  // -> complete component object, no reference to the event?
      // this.$("a.close").click();
    }
  }
});

This works like advertised, however, I need to determine the target of the click event, because also clicks on the item and form will trigger this click(close) action.

Question: How can I access the (jQuery) event object which has a target from within the close action inside the component?

I am using EmberCLI, and Ember 1.9

ahmeij

I have found this to provide the required result:

export default Ember.Component.extend({
  classNames: ['overlay-block'],
  didInsertElement: function() {
    var self = this;

    self.$().click(function(e) {
      if (self.$(e.target).hasClass("overlay-block")) {
        self.$("a.close").click();
      }
    });
  }
});

This does not use an ember action like I expected. I'll leave the question open for a while to see if somebody comes up with an more 'Ember way' of doing this.

More Ember way

export default Ember.Component.extend({
  classNames: ['overlay-block'],
  click: function(e) {
    if (this.$(e.target).hasClass("overlay-block")){
      this.$("a.close").click();
    }
  }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Access jquery event from ember component action

From Dev

Ember: Bubble action from component to application controller

From Dev

Calling controller action from Ember 2.0 component

From Dev

Ember: send action from one component to another

From Dev

Ember: send action from one component to another

From Dev

Sending an action from a sub component to parent component in Ember 2.2

From Dev

Ember Component Action Not Bubbling

From Dev

Access component state from redux action

From Dev

How to send an action from a Ember component to a View that wraps it?

From Dev

Sending action from component to route in ember.js

From Dev

How to send an action from a Ember component to a View that wraps it?

From Dev

Call action from parent component in Ember 2.x

From Dev

ember.js | How to bind an event of a sub-component to an action of an outer component

From Dev

ember.js | How to bind an event of a sub-component to an action of an outer component

From Dev

Ember nested component action not bubbling

From Dev

how to access parent component scope from a child components scope in ember?

From Dev

how to access parent component scope from a child components scope in ember?

From Dev

How to access jquery function properties from event

From Dev

jQuery Plugin: how to access from an event?

From Dev

How to access jquery function properties from event

From Dev

Ember 2 action sent to route instead of component

From Dev

Ember: globally-available search component (and action)?

From Dev

Ember component function outside of action function

From Dev

Ember.js map action keyUp event

From Dev

Nothing handles action on immediate event - Ember

From Dev

Ember.js map action keyUp event

From Dev

Nothing handles action on immediate event - Ember

From Dev

Ember bind action from property

From Dev

How can I catch an enter event from an Ember Input in my component?

Related Related

  1. 1

    Access jquery event from ember component action

  2. 2

    Ember: Bubble action from component to application controller

  3. 3

    Calling controller action from Ember 2.0 component

  4. 4

    Ember: send action from one component to another

  5. 5

    Ember: send action from one component to another

  6. 6

    Sending an action from a sub component to parent component in Ember 2.2

  7. 7

    Ember Component Action Not Bubbling

  8. 8

    Access component state from redux action

  9. 9

    How to send an action from a Ember component to a View that wraps it?

  10. 10

    Sending action from component to route in ember.js

  11. 11

    How to send an action from a Ember component to a View that wraps it?

  12. 12

    Call action from parent component in Ember 2.x

  13. 13

    ember.js | How to bind an event of a sub-component to an action of an outer component

  14. 14

    ember.js | How to bind an event of a sub-component to an action of an outer component

  15. 15

    Ember nested component action not bubbling

  16. 16

    how to access parent component scope from a child components scope in ember?

  17. 17

    how to access parent component scope from a child components scope in ember?

  18. 18

    How to access jquery function properties from event

  19. 19

    jQuery Plugin: how to access from an event?

  20. 20

    How to access jquery function properties from event

  21. 21

    Ember 2 action sent to route instead of component

  22. 22

    Ember: globally-available search component (and action)?

  23. 23

    Ember component function outside of action function

  24. 24

    Ember.js map action keyUp event

  25. 25

    Nothing handles action on immediate event - Ember

  26. 26

    Ember.js map action keyUp event

  27. 27

    Nothing handles action on immediate event - Ember

  28. 28

    Ember bind action from property

  29. 29

    How can I catch an enter event from an Ember Input in my component?

HotTag

Archive