Class: Sirens::Component

Inherits:
AbstractComponent show all
Defined in:
lib/components/component.rb

Instance Method Summary collapse

Methods inherited from AbstractComponent

#add_all_components, #add_component, #child_components, #default_model, #model, #on_model_changed, open, #props, #remove_component_at, #remove_last_component, #set_model, #set_props, #view

Constructor Details

#initialize(props = Hash[]) ⇒ Component

Initializes this component



9
10
11
12
13
# File 'lib/components/component.rb', line 9

def initialize(props = Hash[])
    super(props)

    build
end

Instance Method Details

#buildObject

Configures the widget with its model, styles and child widgets but does not apply the styles yet. This method is called when opening a widget with ::open and after calling ::initialize_handles. The building of the widget includes defining its model, its style props and its event blocks, but does no apply those styles yet. The wiring and synchronization of the component to the widget is done in the ::post_build method.



26
27
28
29
30
31
32
# File 'lib/components/component.rb', line 26

def build()
    set_model( props.key?(:model) ? props[:model] : default_model )

    render_with(LayoutBuilder.new(root_component: self))

    self
end

#create_viewObject



15
16
17
# File 'lib/components/component.rb', line 15

def create_view()
    ComponentView.new
end

#main_child_componentObject

Accessing



37
38
39
# File 'lib/components/component.rb', line 37

def main_child_component()
    @child_components.first
end

#on_component_added(child_component) ⇒ Object

Adds the child_component to this component.



53
54
55
# File 'lib/components/component.rb', line 53

def on_component_added(child_component)
    @view.add_view(child_component.view)
end

#openObject



57
58
59
# File 'lib/components/component.rb', line 57

def open()
    main_child_component.open
end

#render_with(layout) ⇒ Object

Hook method to allow each Component subclass to define its default styles and compose its child components. Subclasses are expected to implement this method.

Raises:

  • (RuntimeError)


46
47
48
# File 'lib/components/component.rb', line 46

def render_with(layout)
    raise RuntimeError.new("Class #{self.class.name} must implement a ::render_with(layout) method.")
end