Description
Currently to declare an event (i.e. a callback that can be attached via the .on
method you need some complicated param declaration syntax like this:
param :on_saved, allow_nil: true, type: Proc, default: nil
# declares the 'saved' event
Whew
Now you can say:
raises :saved
This will create a saved!
method within your component to raise the event.
You can override the internal method name using the alias
option:
`raises :saved, alias: :save_it # defines the save_it method instead of saved!
For non-standard event names you can use the same syntax as the .on
method uses, i.e.
raises '<SAVE>', alias: saved! # you must supply an alias for non-standard names
To attach a SAVE
handler you say `.on('') (as you do today)
For example here is a sample component (see #51 for why there are no more params.
prefixes)
class EditItem
include Hyperstack::Component
include Hyperstack::Observable
param :todo
others :others
raises :save
raises :cancel
after_mount { ::Element[dom_node].focus }
render do
INPUT(
@others,
defaultValue: @todo.title,
placeholder: "What is left to do today?",
key: @todo
)
.on(:key_down) do |evt|
next unless evt.key_code == 13
@todo.update(title: evt.target.value)
save!
end
.on(:blur) { cancel! }
end
end