hyperstack-org / hyperstack-org/hyperstack

add accepts? method

未關閉
#363 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
enhancement
主要語言
JavaScript
星號
538
分支
41
PR 合併指標
30 天內沒有已合併 PR

描述

Its useful for a component to know if its parent is going to accept an event from the component.

```ruby
fires :foo
...
... accepts? :foo # does my parent component have a foo handler?
```

Note that firing foo will NOT cause an error even if the parent has not handler, but it still might be useful to know (i.e. to control say a save button, but only if the parent is looking for you to do the save)

Can be added with the following patch in the HyperComponent base class:

```ruby
class << self
def accepts_list
@accepts_list ||= {}
end
def fires(name, opts = {})
aka = opts[:alias] || "#{name}!"
name = if name =~ /^<(.+)>$/
name.gsub(/^<(.+)>$/, '\1')
elsif Hyperstack::Component::Event::BUILT_IN_EVENTS.include?("on#{name.event_camelize}")
"on#{name.event_camelize}"
else
"on_#{name}"
end
validator.event(name)
define_method(aka) { |*args| props[name]&.call(*args) }
accepts_list[opts[:alias] || aka.chomp('!')] = name
end
end
def accepts?(aka)
props[self.class.accepts_list[aka]]
end
```

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。