Skip to content

Commit

Permalink
Merge pull request #14872 from intercom/gj/further-simplify-event-han…
Browse files Browse the repository at this point in the history
…dler

Simplify action event handler
  • Loading branch information
rwjblue authored Jan 25, 2017
2 parents 177ec98 + b327e87 commit fc729c9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 39 deletions.
22 changes: 2 additions & 20 deletions packages/ember-glimmer/lib/modifiers/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,16 @@ export let ActionHelper = {

registerAction(actionState) {
let { actionId } = actionState;
let actions = ActionManager.registeredActions[actionId];

if (!actions) {
actions = ActionManager.registeredActions[actionId] = [];
}

actions.push(actionState);
ActionManager.registeredActions[actionId] = actionState;

return actionId;
},

unregisterAction(actionState) {
let { actionId } = actionState;
let actions = ActionManager.registeredActions[actionId];

if (!actions) {
return;
}

let index = actions.indexOf(actionState);

if (index !== -1) {
actions.splice(index, 1);
}

if (actions.length === 0) {
delete ActionManager.registeredActions[actionId];
}
delete ActionManager.registeredActions[actionId];
}
};

Expand Down
25 changes: 6 additions & 19 deletions packages/ember-views/lib/system/event_dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,30 +219,17 @@ export default EmberObject.extend({

rootElement.on(`${event}.ember`, '[data-ember-action]', evt => {
let attributes = evt.currentTarget.attributes;
let attributeCount = attributes.length;
let actions = [];

for (let i = 0; i < attributeCount; i++) {
for (let i = 0; i < attributes.length; i++) {
let attr = attributes.item(i);
let attrName = attr.name;

if (attrName.indexOf('data-ember-action-') === 0) {
actions = actions.concat(ActionManager.registeredActions[attr.value]);
}
}

// We have to check for actions here since in some cases, jQuery will trigger
// an event on `removeChild` (i.e. focusout) after we've already torn down the
// action handlers for the view.
if (actions.length === 0) {
return;
}

for (let index = 0; index < actions.length; index++) {
let action = actions[index];
if (attrName.lastIndexOf('data-ember-action-', 0) !== -1) {
let action = ActionManager.registeredActions[attr.value];

if (action && action.eventName === eventName) {
return action.handler(evt);
if (action.eventName === eventName) {
action.handler(evt);
}
}
}
});
Expand Down

0 comments on commit fc729c9

Please sign in to comment.