Skip to content

Commit

Permalink
Sets its name inside the listener function. Fixes #222
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Souza committed Mar 31, 2017
1 parent fa34ecc commit 62f8d28
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/metal-incremental-dom/src/render/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export function convertListenerNamesToFns(component, config) {

/**
* Converts the given attribute's value to a function reference, if it's
* currently a listener name.
* currently a listener name. It also register the listener name for
* further usage.
* @param {!Component} component
* @param {string} name
* @param {*} value
Expand All @@ -95,7 +96,7 @@ function convertListenerNameToFn_(component, name, value) {
const eventName = getEventFromListenerAttr_(name);
if (eventName) {
const fn = getComponentFn(component, value);
fn.givenAsName_ = name;
fn.givenAsName_ = value;
return fn;
}
}
Expand Down
16 changes: 16 additions & 0 deletions packages/metal-incremental-dom/test/IncrementalDomRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,22 @@ describe('IncrementalDomRenderer', function() {
});

describe('Inline Listeners', function() {
it('should shows the listener name in the dom element', function() {
class TestComponent extends Component {
render() {
IncDom.elementOpen('div');
IncDom.elementVoid('div', null, null, 'onClick', 'handleClick');
IncDom.elementClose('div');
}
}
TestComponent.RENDERER = IncrementalDomRenderer;
TestComponent.prototype.handleClick = function() {};

component = new TestComponent();
let elementChild = component.element.querySelector('div');
assert.strictEqual('handleClick', elementChild.dataset.onclick);
});

it('should attach listeners from "on<EventName>" attributes', function() {
class TestComponent extends Component {
render() {
Expand Down
16 changes: 15 additions & 1 deletion packages/metal-incremental-dom/test/render/attributes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { applyAttribute } from '../../src/render/attributes';
import { applyAttribute, convertListenerNamesToFns } from '../../src/render/attributes';
import dom from 'metal-dom';
import Component from 'metal-component';

Expand Down Expand Up @@ -50,6 +50,20 @@ describe('attributes', function() {
});

describe('listeners', function() {
it('should register the method name inside the listener', function() {
class TestComponent extends Component {
}
TestComponent.prototype.handleClick = function() {};
component = new TestComponent();

let config = {
'data-onclick': 'handleClick'
};

convertListenerNamesToFns(component, config);
assert.strictEqual('handleClick', config['data-onclick'].givenAsName_);
});

it('should attach listeners functions passed to "data-on<eventname>" attributes', function() {
class TestComponent extends Component {
}
Expand Down

0 comments on commit 62f8d28

Please sign in to comment.