Last active
August 29, 2015 14:14
-
-
Save h-sao/2af7d4d39a7222223868 to your computer and use it in GitHub Desktop.
VirtualDOMのハンズオン:index.html(アラートが出る、bodyの下にdiv>divが出来る)
This file contains 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 xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<title></title> | |
<script src="virtualdom.js"></script> | |
</head> | |
<body> | |
<div> | |
<input type="text" name="todo" /> | |
<button name="add" type="button">add</button> | |
</div> | |
<div class="list"> | |
</div> | |
<script> | |
var todos = [], | |
textbox = document.querySelector("[name=todo]"); // Textbox | |
document.querySelector("[name=add]").addEventListener("click", function () { | |
alert(textbox.value); | |
}, false) | |
// ここでVirtualDOMを組み立てる | |
function createVDom(todolist) { | |
var textVDoms = todolist.map(function (todo) { | |
return virtualDom.h("div", {}, [ | |
virtualDom.h("span", {}, todo) | |
]); | |
}); | |
return virtualDom.h("div", {}, textVDoms); | |
} | |
// createVDom()では、JavaScriptの世界で勝手に作ったDOMが出来ただけ | |
// これを、実際のHTMLが判断できるDOMにしてあげる必要がある | |
var todoVDOM = createVDom(todos); // VirtualDOM作成 | |
var todoNode = virtualDom.create(todoVDOM); // DOM作成 | |
document.querySelector(".list").appendChild(todoNode); // DOM Treeに追加 | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
virtualdom.js はここ
https://raw.githubusercontent.com/Matt-Esch/virtual-dom/master/dist/virtual-dom.js