Last active
December 28, 2015 15:29
-
-
Save ishiduca/7522276 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
| ;(function (global) { | |
| 'use strict' | |
| var isBrowser = !! global.self | |
| var isWorker = !! global.WorkerLocation | |
| var isNodeJS = !! global.global | |
| var emitter = {} | |
| emitter.constructor = function constructor () { | |
| this.evs = {} | |
| return this | |
| } | |
| emitter.on = emitter.addListener = function (name, listener) { | |
| ;(this.evs[name] || (this.evs[name] = [])).push(listener) | |
| return this | |
| } | |
| emitter.once = function (name, listener) { | |
| var me = this | |
| function _remove () { | |
| listener.apply(null, arguments) | |
| me.off(name, _remove) | |
| } | |
| return this.on(name, _remove) | |
| } | |
| emitter.emit = function () { | |
| var args = [].slice.apply(arguments) | |
| var name = args.shift() | |
| var evs = this.evs[name] || [] | |
| var flg = false | |
| for (var i = 0, l = evs.length; i < l; i++) { | |
| typeof evs[i] === 'function' && (flg = true) && evs[i].apply(null, args) | |
| } | |
| return flg | |
| } | |
| emitter.off = emitter.removeListener = function (name, listener) { | |
| //this.evs[name] && (this.evs[name][ this.evs[name].indexOf(listener) ] = null) | |
| this.evs[name] && (this.evs[name][ indexOf(this.evs[name], listener) ] = null) | |
| return this | |
| } | |
| if (isNodeJS) { | |
| module.exports = emitter | |
| module.exports.constructor.prototype = module.exports | |
| } else { | |
| global.emitter = emitter | |
| global.emitter.constructor.prototype = emitter | |
| } | |
| })(this.self || global) | |
| function indexOf (arry, search) { | |
| if (typeof arry.indexOf === 'function') { | |
| return arry.indexOf(search) | |
| } | |
| for (var i = 0, len = arry.length; i < len; i++) { | |
| if (arry[i] === search) return i | |
| } | |
| return -1 | |
| } |
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> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <link rel="stylesheet" href="./bower-components/normalize-css/normalize.css" type="text/css" /> | |
| <style> | |
| #contents { | |
| margin: 0 auto; | |
| padding: 20px; | |
| } | |
| ul { | |
| list-style: none; | |
| padding: 0 | |
| } | |
| #errors { | |
| padding: 10px; | |
| height: 1em; | |
| width: 100%; | |
| z-index: 2; | |
| position: fixed; | |
| top: 0; | |
| left: 0; | |
| } | |
| </style> | |
| <script src="./bower-components/evem-min/evem-min.js"></script> | |
| <title>ToDo Sample - MVC</title> | |
| </head> | |
| <body> | |
| <div id="sub-contents"><div id="errors"></div></div> | |
| <div id="contents"> | |
| <h1>ToDo Sample</h1> | |
| <form id="task-add" action="javascript:void(0)"> | |
| <input type="text" id="new-task-name" placeholder="input your task" /> | |
| </form> | |
| <ul id="tasks"></ul> | |
| </div> | |
| </body> | |
| <script> | |
| 'use strict' | |
| emitter.inherits = function (Cnst) { | |
| var F = function () {} | |
| F.prototype = this | |
| Cnst.prototype = new F | |
| Cnst.prototype.constructor = Cnst | |
| } | |
| this.onload = function init () { | |
| var d = document | |
| var View = {} | |
| var Model = {} | |
| Model.Task = function (title) { | |
| emitter.constructor.call(this) | |
| if (! title) throw new Error('"title" not found') | |
| this.title = title | |
| this.done = false | |
| return this | |
| } | |
| emitter.inherits(Model.Task) | |
| Model.Tasks = function () { | |
| emitter.constructor.call(this) | |
| this.models = [] | |
| return this | |
| } | |
| emitter.inherits(Model.Tasks) | |
| Model.Tasks.prototype.add = function (model) { | |
| this.models.unshift(model) | |
| this.emit('add', model) | |
| return model | |
| } | |
| View.TaskList = function (tasks, node) { | |
| emitter.constructor.call(this) | |
| this.tasks = tasks | |
| this.node = node | |
| var tasklist = this | |
| tasks.on('add', function (task) { | |
| tasklist.render() | |
| }) | |
| tasks.on('change', function () { | |
| tasklist.render() | |
| }) | |
| return this | |
| } | |
| emitter.inherits(View.TaskList) | |
| View.TaskList.prototype.render = function () { | |
| var node = this.node | |
| var tasks = this.tasks | |
| node.innerHTML = '' | |
| each(tasks.models, function (task) { | |
| var $item = d.createElement('li') | |
| !! task.done && ($item.style.backgroundColor = '#eeeeee') | |
| var $input = d.createElement('input') | |
| $input.setAttribute('type', 'checkbox') | |
| $input.checked = !! task.done | |
| $input.onclick = function (e) { | |
| task.done = !! $input.checked | |
| //task.done = !! e.target.checked | |
| tasks.emit('change') | |
| } | |
| $item.appendChild($input) | |
| $item.appendChild(d.createTextNode(' ' + task.title)) | |
| node.appendChild($item) | |
| }) | |
| } | |
| View.Errors = function (node) { | |
| emitter.constructor.call(this) | |
| this.node = node | |
| this.timout = 2000 | |
| var that = this | |
| this.on('error',function (err) { | |
| that.info(err) | |
| }) | |
| this.init() | |
| return this | |
| } | |
| emitter.inherits(View.Errors) | |
| View.Errors.prototype.info = function (err) { | |
| this.node.style.backgroundColor = '#ffaa00' | |
| this.node.innerHTML = err.toString() | |
| var that = this | |
| setTimeout(function () { | |
| that.init() | |
| }, this.timout) | |
| } | |
| View.Errors.prototype.init = function () { | |
| this.node.style.backgroundColor = '' | |
| this.node.innerHTML = '' | |
| } | |
| // model, view init | |
| var tasks = new Model.Tasks() | |
| var taskList = new View.TaskList(tasks, d.querySelector('#tasks')) | |
| var errs = new View.Errors(d.querySelector('#errors')) | |
| each(('郵便を出す 公開中の映画タイトルを見ておく オムレツを極める').split(' '), function (t) { | |
| tasks.add(new Model.Task(t)) | |
| }) | |
| // controller init | |
| var $taskAddForm = d.querySelector('#task-add') | |
| var $newTaskNameForm = d.querySelector('#new-task-name') | |
| $taskAddForm.onsubmit = function (e) { | |
| try { | |
| tasks.add(new Model.Task($newTaskNameForm.value)) | |
| $newTaskNameForm.value = '' | |
| return false | |
| } catch (err) { | |
| errs.emit('error', err) | |
| } | |
| } | |
| $newTaskNameForm.focus() | |
| } | |
| function each (arry, cb) { | |
| for (var i = 0, len = arry.length; i < len; i++) { | |
| cb(arry[i], i) | |
| } | |
| } | |
| </script> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ie8 対応までやった。。。 おかげでノイズが乗っちゃったな