hyperstack icon indicating copy to clipboard operation
hyperstack copied to clipboard

add accepts? method

Open catmando opened this issue 4 years ago • 2 comments

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

   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:

  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

catmando avatar Feb 26 '21 07:02 catmando

#247 might be an easier to understand.

catmando avatar Apr 05 '21 04:04 catmando

I think it should be: def accepts_list @@accepts_list ||= {} end with @@ for class variable

mpantel avatar Oct 21 '21 05:10 mpantel