admin管理员组

文章数量:1323336

Wondering if anyone has e up with a better way for handling a click outside a div while using Ember? I know of the jQuery way with a global click handler that you must specify each action to take for certain instances, but am hoping someone has e up with a way to declare this inside an Ember view. As well, I tried the ol' give a div a tab index and use the on blur, but Ember actions don't seem to allow this.

Wondering if anyone has e up with a better way for handling a click outside a div while using Ember? I know of the jQuery way with a global click handler that you must specify each action to take for certain instances, but am hoping someone has e up with a way to declare this inside an Ember view. As well, I tried the ol' give a div a tab index and use the on blur, but Ember actions don't seem to allow this.

Share asked Apr 24, 2013 at 22:56 chris brownchris brown 1611 silver badge8 bronze badges 2
  • 1 I don't understand what the issue is. You can delegate the event based on a regexp expression, this doesn't require anything crazy. Even taking a look at the Ember.js documentation states that leveraging jQuery's .on() method is the approach they use to solving delegation issues along large subsets of elements, nested or not. – Ohgodwhy Commented Apr 24, 2013 at 23:08
  • 1 You can use the didInsertElement event to start up a plug-in and things like that. And you should use willDestroyElement to remove bindings. Alternatively you can use View#on. – MilkyWayJoe Commented Apr 25, 2013 at 0:15
Add a ment  | 

2 Answers 2

Reset to default 7

Thanks for the input. I went back and read the documentation on jQuerys .on again. I was not aware that you could namespace the event. So I took both of the ments and bined them with something like this.

didInsertElement: function() {
    Ember.run.next(this, 'attachClickHandler');
},

attachClickHandler: function (){
    var temp = this;

    $(window).on("click." + temp.elementId, function (e){
        //...event here
    });
},

detachClickHandler: function (){
    $(window).off("click." + this.elementId);
},

This allows for the event to be specific for each view instance, which is what I was wanting. Thanks guys!

you may find ClickElsewhereMixin useful

just include the mixin on any ponent and implement onClickElsewhere()

本文标签: javascriptEmberHandling clicks outside of viewStack Overflow