Class: Apotomo::Widget
- Inherits:
-
Cell::Rails
- Object
- Cell::Rails
- Apotomo::Widget
- Includes:
- EventMethods, JavascriptMethods, TreeNode, WidgetShortcuts, Hooks, Onfire
- Defined in:
- lib/apotomo/widget.rb
Overview
Accessing Parameters
Apotomo tries to prevent you from having to access the global #params hash. We have the following concepts to retrieve input data.
-
Configuration values are available both in render and triggered states. Pass those in #widget
when creating the widget tree. Use #options for reading.
has_widgets do |root|
root << widget(:mouse_widget, 'mum', :favorites => ["Gouda", "Chedar"])
and read in your widget state
def display
@cheese = options[:favorites].first
-
Request data from forms etc. is available through
event.data
in the triggered states.
Use the #[]
shortcut to access values directly.
def update(evt)
@cheese = Cheese.find evt[:cheese_id]
Instance Attribute Summary collapse
-
#name ⇒ Object
(also: #widget_id)
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#visible ⇒ Object
writeonly
Sets the attribute visible.
Attributes included from EventMethods
Attributes included from TreeNode
Class Method Summary collapse
Instance Method Summary collapse
- #address_for_event(type, options = {}) ⇒ Object
-
#find_widget(widget_id) ⇒ Object
Returns the widget named
widget_id
if it’s a descendent or self. -
#initialize(parent, id, options = {}) ⇒ Widget
constructor
A new instance of Widget.
-
#invoke(state, *args) ⇒ Object
Invokes
state
and hopefully returns the rendered content. - #parent_controller ⇒ Object
-
#render(*args, &block) ⇒ Object
Renders and returns a view for the current state.
-
#render_widget(widget_id, state = :display, *args) ⇒ Object
Renders the
widget
(instance or id). - #url_for_event(type, options = {}) ⇒ Object
- #visible? ⇒ Boolean
Methods included from JavascriptMethods
Methods included from WidgetShortcuts
Methods included from EventMethods
#handlers_for_event, #respond_to_event, #trigger
Methods included from TreeNode
#<=>, #[], #add_widget, #children, #each, #find_by_path, #path, #printTree, #remove!, #root, #root?, #setup_tree_node, #size, #to_s
Methods included from Apotomo::WidgetShortcuts::DSL
Constructor Details
#initialize(parent, id, options = {}) ⇒ Widget
Returns a new instance of Widget.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/apotomo/widget.rb', line 81 def initialize(parent, id, ={}) super(parent) # TODO: do that as long as cells do need a parent_controller. @options = @name = id @visible = true setup_tree_node(parent) run_hook :after_initialize, self end |
Instance Attribute Details
#name ⇒ Object (readonly) Also known as: widget_id
Returns the value of attribute name.
71 72 73 |
# File 'lib/apotomo/widget.rb', line 71 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
74 75 76 |
# File 'lib/apotomo/widget.rb', line 74 def @options end |
#visible=(value) ⇒ Object (writeonly)
Sets the attribute visible
55 56 57 |
# File 'lib/apotomo/widget.rb', line 55 def visible=(value) @visible = value end |
Class Method Details
.controller_path ⇒ Object
144 145 146 |
# File 'lib/apotomo/widget.rb', line 144 def self.controller_path @controller_path ||= name.sub(/Widget$/, '').underscore unless anonymous? end |
Instance Method Details
#address_for_event(type, options = {}) ⇒ Object
133 134 135 136 137 |
# File 'lib/apotomo/widget.rb', line 133 def address_for_event(type, ={}) .reverse_merge! :source => name, :type => type, :controller => parent_controller.controller_path # DISCUSS: dependency to parent_controller. end |
#find_widget(widget_id) ⇒ Object
Returns the widget named widget_id
if it’s a descendent or self.
129 130 131 |
# File 'lib/apotomo/widget.rb', line 129 def () find {|node| node.name.to_s == .to_s} end |
#invoke(state, *args) ⇒ Object
Invokes state
and hopefully returns the rendered content.
102 103 104 105 |
# File 'lib/apotomo/widget.rb', line 102 def invoke(state, *args) return render_state(state, *args) if method(state).arity != 0 # TODO: remove check and make trigger states receive the evt default. render_state(state) end |
#parent_controller ⇒ Object
92 93 94 95 |
# File 'lib/apotomo/widget.rb', line 92 def parent_controller # i hope we'll get rid of any parent_controller dependency, soon. root? ? @parent_controller : root.parent_controller end |
#render(*args, &block) ⇒ Object
Renders and returns a view for the current state. That’s why it is usually called at the end of a state method.
Options
Example:
class MouseWidget < Apotomo::Widget
def eat
render
end
render the view eat.haml
.
render :text => "alert('SQUEAK!');"
issues a squeaking alert dialog on the page.
124 125 126 |
# File 'lib/apotomo/widget.rb', line 124 def render(*args, &block) super end |
#render_widget(widget_id, state = :display, *args) ⇒ Object
Renders the widget
(instance or id).
149 150 151 152 153 154 155 156 157 |
# File 'lib/apotomo/widget.rb', line 149 def (, state=:display, *args) if .kind_of?(Widget) = else = () or raise "Couldn't render non-existent widget `#{}`" end .invoke(state, *args) end |
#url_for_event(type, options = {}) ⇒ Object
139 140 141 |
# File 'lib/apotomo/widget.rb', line 139 def url_for_event(type, ={}) apotomo_event_path address_for_event(type, ) end |
#visible? ⇒ Boolean
97 98 99 |
# File 'lib/apotomo/widget.rb', line 97 def visible? @visible end |