Class: Apotomo::Widget
- Inherits:
-
Cell::Base
- Object
- Cell::Base
- Apotomo::Widget
- Includes:
- EventMethods, JavascriptMethods, WidgetShortcuts, Hooks, Onfire, TreeNode
- 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]
Constant Summary collapse
- DEFAULT_VIEW_PATHS =
[ File.join('app', 'widgets'), File.join('app', 'widgets', 'layouts') ]
Instance Attribute Summary collapse
-
#visible ⇒ Object
writeonly
Sets the attribute visible.
Attributes included from EventMethods
Attributes included from TreeNode
Class Method Summary collapse
Instance Method Summary collapse
- #add_has_widgets_blocks ⇒ Object
- #address_for_event(type, options = {}) ⇒ Object
-
#find_widget(widget_id) ⇒ Object
Returns the widget named
widget_id
as long as it is below self or self itself. -
#initialize(parent_controller, id, options = {}) ⇒ Widget
constructor
A new instance of Widget.
-
#invoke(state, *args) ⇒ Object
Invokes
state
and hopefully returns the rendered content. - #param(name) ⇒ Object
-
#render(*args, &block) ⇒ Object
(also: #emit)
Render the view for the current state.
-
#render_widget(widget_id, state = :display, *args) ⇒ Object
Renders the
widget
(instance or id). -
#run_widget_hook(name, *args) ⇒ Object
Runs callbacks for
name
hook in instance context. - #url_for_event(type, options = {}) ⇒ Object
- #visible? ⇒ Boolean
Methods included from JavascriptMethods
Methods included from WidgetShortcuts
Methods included from EventMethods
#add_class_event_handlers, #respond_to_event, #trigger
Methods included from TreeNode
#<<, #<=>, #[], #add, #children, #each, #find_by_path, included, #initialize_tree_node, #path, #printTree, #remove!, #remove_all!, #remove_from_parent!, #root, #root?, #size, #to_s
Constructor Details
#initialize(parent_controller, id, options = {}) ⇒ Widget
Returns a new instance of Widget.
92 93 94 95 96 97 98 99 |
# File 'lib/apotomo/widget.rb', line 92 def initialize(parent_controller, id, ={}) super(parent_controller, ) # do that as long as cells do need a parent_controller. @name = id @visible = true run_hook :after_initialize, self end |
Instance Attribute Details
#visible=(value) ⇒ Object (writeonly)
Sets the attribute visible
61 62 63 |
# File 'lib/apotomo/widget.rb', line 61 def visible=(value) @visible = value end |
Class Method Details
.controller_path ⇒ Object
160 161 162 |
# File 'lib/apotomo/widget.rb', line 160 def self.controller_path @controller_path ||= name.sub(/Widget$/, '').underscore unless anonymous? end |
Instance Method Details
#add_has_widgets_blocks ⇒ Object
86 87 88 |
# File 'lib/apotomo/widget.rb', line 86 def (*) (:has_widgets, self) end |
#address_for_event(type, options = {}) ⇒ Object
149 150 151 152 153 |
# File 'lib/apotomo/widget.rb', line 149 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
as long as it is below self or self itself.
145 146 147 |
# File 'lib/apotomo/widget.rb', line 145 def () find {|node| node.name.to_s == .to_s} end |
#invoke(state, *args) ⇒ Object
Invokes state
and hopefully returns the rendered content.
106 107 108 109 |
# File 'lib/apotomo/widget.rb', line 106 def invoke(state, *args) return render_state(state, *args) if state_accepts_args?(state) render_state(state) end |
#param(name) ⇒ Object
137 138 139 140 141 |
# File 'lib/apotomo/widget.rb', line 137 def param(name) msg = "Deprecated. Use #options for widget constructor options or #params for request data." ActiveSupport::Deprecation.warn(msg) raise msg end |
#render(*args, &block) ⇒ Object Also known as: emit
Render the view for the current state. Usually called at the end of a state method.
Options
-
:view
- Specifies the name of the view file to render. Defaults to the current state name. -
:template_format
- Allows using a format different to:html
. -
:layout
- If set to a valid filename inside your cell’s view_paths, the current state view will be rendered inside the layout (as known from controller actions). Layouts should reside inapp/cells/layouts
. -
see Cell::Base#render for additional options
Example:
class MouseWidget < Apotomo::StatefulWidget
def eat
# ... do something
render
end
will just render the view eat.haml
.
render :js => "alert('SQUEAK!');"
issues a squeaking alert dialog on the page.
131 132 133 |
# File 'lib/apotomo/widget.rb', line 131 def render(*args, &block) super end |
#render_widget(widget_id, state = :display, *args) ⇒ Object
Renders the widget
(instance or id).
165 166 167 168 169 170 171 172 173 |
# File 'lib/apotomo/widget.rb', line 165 def (, state=:display, *args) if .kind_of?(Widget) = else = () or raise "Couldn't render non-existent widget `#{}`" end .invoke(state, *args) end |
#run_widget_hook(name, *args) ⇒ Object
Runs callbacks for name
hook in instance context.
82 83 84 |
# File 'lib/apotomo/widget.rb', line 82 def (name, *args) self.class.callbacks_for_hook(name).each { |blk| instance_exec(*args, &blk) } end |
#url_for_event(type, options = {}) ⇒ Object
155 156 157 |
# File 'lib/apotomo/widget.rb', line 155 def url_for_event(type, ={}) apotomo_event_path address_for_event(type, ) end |
#visible? ⇒ Boolean
101 102 103 |
# File 'lib/apotomo/widget.rb', line 101 def visible? @visible end |