Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify action event handler #14872

Merged
merged 1 commit into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it ok to remove the if(action && guard here? Do we care about supporting non existent actions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is probably fine. I can't see how we would end up here with an action that is falsey without having already screwed something majorly up elsewhere.

action.handler(evt);
}
}
}
});
Expand Down