absqueued / Context-Menu-Angular-Directive

Custom context created for angular js apps.
http://shekhardesigner.github.io/Context-Menu-Angular-Directive/
MIT License
9 stars 3 forks source link

Use closest instead of find for pointerNode #3

Open bjarnef opened 8 years ago

bjarnef commented 8 years ago

In my example for https://github.com/shekhardesigner/Context-Menu-Angular-Directive/issues/2 I have added pointer-node on same element as context-menu

<td ng-repeat="column in row" class="cell" data-rowindex="{{$parent.$index}}" context-menu menu-items="menus" pointer-node=".cell" table-editor-column-control>
...
</td>

However find() is only looking for descendants https://api.jquery.com/find/ therefore it returns undefined.

image

By using closest() it get the first element that matches the selector by testing the element itself and traversing up through its ancestors.

When I in L157 change to .closest() instead if .find()it works in my case. https://github.com/shekhardesigner/Context-Menu-Angular-Directive/blob/master/app/scripts/context-menu.js#L157

image

bjarnef commented 8 years ago

Angular's built-in subset of jQuery, called "jQuery lite" does not has closest() and find() is limited to look-up by tag name: https://docs.angularjs.org/api/ng/function/angular.element

So since it rely on use of jQuery it should be fine to use closest().

Alternate use addBack() which allow to include itself: http://stackoverflow.com/a/17538213

var $pointer = $(this).find($attr.pointerNode).addBack($attr.pointerNode);

or

var $pointer = $(this).closest($attr.pointerNode);