Last active
August 29, 2015 14:04
-
-
Save negipo/595d5c014838be1bc863 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
# http://hokaccha.github.io/slides/javascript_design_and_test/ の写経 | |
class Todo | |
@list: [] | |
@add: (text) -> | |
todo = new Todo(text: text) | |
Todo.list.push(todo) | |
@trigger('add', todo) | |
constructor: (opts) | |
@text = opts.text | |
setComplete: (complete) | |
@complete | |
@trigger('change:complete', this) | |
# Todoを入力するフォームを管理するViewクラス | |
class TodoFormView | |
constructor: (el) | |
@el = el | |
@input = el.find('input[type="text"]') | |
@el.submit(@onsubmit.bind(@)) | |
onsubmit: (e) -> | |
e.preventDefault() | |
Todo.add(@input.val()) | |
# Todo一覧のリストを管理するViewクラス | |
class TodoListView | |
constructor: (el) | |
@el = el | |
# つまり、viewを生成するコードはmodelには無いが、modelのイベントにフックする形でViewクラスに書く | |
Todo.on('add', @add.bind(@)) | |
add: (todo) -> | |
item = new TodoListItemView(todo) | |
@el.append(item.el) | |
# Todo一覧の要素を管理 | |
class TodoListItemView | |
consturctor: (todo) -> | |
@todo = todo | |
@el = $("<li><input type='checkbox'>#{todo.text}</li>") | |
@checkbox = @el.find('input[type="checkbox"]') | |
@checkbox.change(@onchangeCheckbox.bind(@)) | |
@todo.on('change:complete', @onchangeComplete.bind(@)) | |
onchangeCheckbox: -> | |
@todo.setComplete(@checkbox.is(':checked')) | |
onchangeComplete: -> | |
if @todo.complete | |
@el.addClass('complete') | |
else | |
@el.removeClass('complete') | |
@checkbox.attr('checked', @todo.compelete) | |
# main.js | |
jQuery(($)-> | |
new TodoFormView($('.todo_form')) | |
new TodoListView($('.todo_list')) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
triggerとかonとかはbackbone.jsのやつ
http://backbonejs.org/backbone.js
まあ簡単だね