Skip to content

Instantly share code, notes, and snippets.

@chikamichi
Created February 24, 2011 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chikamichi/842193 to your computer and use it in GitHub Desktop.
Save chikamichi/842193 to your computer and use it in GitHub Desktop.
an example of Apotomo structure
class MyController < ApplicationController
include Apotomo::Rails::ControllerMethods
has_widgets do |root|
if must_attach_a_wigdet?
# current_widget_data is anything that the Cell needs to build
# the widget (say, a User record…)
root << widget('my_widget',
"widget_#{@current_widget_data.id}",
:widget_data_id => @current_widget_data.id)
end
end
def index
# …
end
private
# This checks whether a @widget has been provided by the current action,
# or required through the params hash. This enables the has_widgets block
# to access the required data to attach a widget to the controller.
# "widget" here may be any piece of data required to actually build the
# cell.
def must_attach_a_widget?
@current_widget_data = @current_widget_data || Data.find(params[:widget_data_id])
end
end
module MyWidgets
class MyWidget < Apotomo::Widget
responds_to_event :change, :with => :refresh
def display
setup!
render
end
def refresh
setup!
end
private
# Retrieve the required object/data, using either the id passed as a local
# in the has_widget block.
#
def setup!
@widget_data = Data.find options[:widget_data_id]
end
end
end
# In the view, call render_widget if MyWidget has children to rerender them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment