Skip to content

Commit

Permalink
改成了老式的 on(:save) 写法, 工作了.
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Jul 30, 2017
1 parent f3376e9 commit 52e3ede
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
8 changes: 4 additions & 4 deletions app/hyperloop/components/edit_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class EditItem < Hyperloop::Component
# 而下面的 on_??? 则是定义了两个 `自定义事件',
# type: Proc 表示这个名称的 param 是个 event 类型的 params
# 当这个 params.on_save 被调用时, 该事件的 handler(Proc对象) 会被执行.
param :on_save
param :on_cancel
param :on_save, type: Proc
param :on_cancel, type: Proc

# dom_node 方法返回一个组件被 mount 成功后, 这个组件对应的 node.
# 当 EditItem 组件 mount 成功后, 设定焦点.
Expand All @@ -17,10 +17,10 @@ class EditItem < Hyperloop::Component
edit_input.on(:key_down) do |evt|
next unless evt.key_code == 13
params.todo.update(title: evt.target.value)
params.on_save.call # 激活 on_save 自定义事件.
params.on_save # 激活 on_save 自定义事件.
# 因为声明的时候, 使用了 type: Proc, 意味着这个调用会自动在 params.on_save 之上调用 call 方法.
end
edit_input.on(:blur) { params.on_cancel.call } # add
edit_input.on(:blur) { params.on_cancel } # add
# edit_input.on(:blur) do |evt|
# params.todo.update(title: evt.target.value)
# end
Expand Down
6 changes: 1 addition & 5 deletions app/hyperloop/components/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ class Header < Hyperloop::Component
state(:new_todo) { Todo.new }

render(HEADER) do
EditItem(
todo: state.new_todo,
on_save: proc { puts 'hello'; mutate.new_tudo Todo.new },
on_cancel: proc {}
)
EditItem(todo: state.new_todo).on(:save) { mutate.new_todo Todo.new }
end
end
8 changes: 3 additions & 5 deletions app/hyperloop/components/todo_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ class TodoItem < Hyperloop::Component
# 这里为自定义 on_save/on_cancel 事件绑定了 handler 方法.
# 此时, EditItem 的 params.on_save 返回的是 handler 对象.(一个 Proc)

EditItem(
todo: params.todo,
on_save: proc { mutate.editing false },
on_cancel: proc { mutate.editing false }
)
EditItem(todo: params.todo).on(:save, :cancel) do
mutate.editing false
end
else
INPUT(type: :checkbox, checked: params.todo.completed).on(:click) do
params.todo.update(completed: !params.todo.completed)
Expand Down

0 comments on commit 52e3ede

Please sign in to comment.