Skip to content

Commit

Permalink
fix(bind): when no initial context value
Browse files Browse the repository at this point in the history
was throwing error when multiple bindings on same undefined context property
  • Loading branch information
MrAntix committed May 4, 2019
1 parent 339449b commit cd247cb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"build": "tsc",
"build.docs": "typedoc --out docs --mode file src --theme minimal --readme docs.md --media dist",
"clean": "shx rm -rf dist",
"test": "jest ",
"test": "jest",
"test.watch": "jest --watch",
"commit": "npx git-cz",
"semantic-release": "semantic-release"
},
Expand Down
24 changes: 23 additions & 1 deletion src/bind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,35 @@ describe('bind', () => {
expect(els[0].innerHTML).toEqual(context.html);
expect(els[1].innerHTML).toEqual(context.html);

const update ='<span>updated</span>';
const update = '<span>updated</span>';
context.html = update;

expect(els[0].innerHTML).toEqual(update);
expect(els[1].innerHTML).toEqual(update);
});

it('binds attributes when no context property', () => {
document.documentElement.innerHTML = `
<div bind [title]="title"></div>
<div bind [title]="title"></div>
`;

const context: any = {};

bind(document, context);

const els = document.querySelectorAll<HTMLElement>('[bind]');

expect(els[0].innerHTML).toEqual('');
expect(els[1].innerHTML).toEqual('');

const update = 'update';
context.title = update;

expect(els[0].title).toEqual(update);
expect(els[1].title).toEqual(update);
});

it('binds events', async () => {
document.documentElement.innerHTML = '<div bind {click}="onClick"></div>';

Expand Down
4 changes: 3 additions & 1 deletion src/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function bind(rootElement: Document | Element, context: any): void {

boundFunction();
} else {

let getValue = descriptor.get || (() => descriptor.value);
let setValue = descriptor.set || (value => (descriptor.value = value));

Expand All @@ -51,7 +52,8 @@ export function bind(rootElement: Document | Element, context: any): void {
}
if (getValue() !== value) setValue(value);
},
get: getValue
get: getValue,
configurable: true
});

component[binding.componentMemberName] = getValue.bind(element)();
Expand Down

0 comments on commit cd247cb

Please sign in to comment.