Created
December 22, 2016 14:37
-
-
Save tanishiking/9cdc310da69d504a893220848b1753f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
<title>TypeScript test</title> | |
</head> | |
<body> | |
<div class="container"> | |
<ul class="piyo-list"> | |
<li><a href="" class="piyo-button">Add piyo</a></li> | |
</ul> | |
</div> | |
<script src="../js/app.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/ts/index.ts | |
import { Piyo } from "./piyo"; | |
function init() { | |
const container = <HTMLElement>document.querySelector('.container'); | |
new Piyo(container); | |
} | |
export function getPiyo(): string { | |
return 'piyo'; | |
} | |
document.addEventListener('DOMContentLoaded', init); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/ts/piyo.ts | |
export class Piyo { | |
private list: HTMLElement; | |
private button: HTMLElement; | |
constructor(container: HTMLElement) { | |
this.list = <HTMLElement>container.querySelector('.piyo-list') | |
this.button = <HTMLElement>container.querySelector('.piyo-button'); | |
this.button.addEventListener('click', this.onButtonClicked.bind(this)); | |
} | |
private onButtonClicked(event: Event): void { | |
event.preventDefault(); | |
this.appendList(this.list, 'piyo'); | |
} | |
private appendList(list: HTMLElement, text: string) { | |
const item = document.createElement('li'); | |
item.innerHTML = text; | |
list.appendChild(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment