lib/apotomo/widget.rb in apotomo-0.1.2 vs lib/apotomo/widget.rb in apotomo-0.1.3
- old
+ new
@@ -1,80 +1,62 @@
+require 'cells'
require 'onfire'
-require 'apotomo/tree_node'
+require 'hooks'
-
+require 'apotomo/tree_node'
require 'apotomo/event'
require 'apotomo/event_methods'
require 'apotomo/transition'
require 'apotomo/caching'
-require 'apotomo/deep_link_methods'
require 'apotomo/widget_shortcuts'
require 'apotomo/rails/view_helper'
-### TODO: use load_hooks when switching to rails 3.
-# wycats@gmail.com: ActiveSupport.run_load_hooks(:name)
-# (21:01:17) wycats@gmail.com: ActiveSupport.on_load(:name) { … }
-#require 'active_support/lazy_load_hooks'
-
module Apotomo
class Widget < Cell::Base
+ include Hooks
- class_inheritable_array :initialize_hooks, :instance_writer => false
- self.initialize_hooks = []
+ # Use this for setup code you're calling in every state. Almost like a +before_filter+ except that it's
+ # invoked after the initialization in #has_widgets.
+ #
+ # Example:
+ #
+ # class MouseWidget < Apotomo::Widget
+ # after_initialize :setup_cheese
+ #
+ # # we need @cheese in every state:
+ # def setup_cheese(*)
+ # @cheese = Cheese.find @opts[:cheese_id]
+ define_hook :after_initialize
+ define_hook :has_widgets
+ define_hook :after_add
attr_accessor :opts
attr_writer :visible
attr_writer :controller
attr_accessor :version
- ### DISCUSS: extract to has_widgets_methods for both Widget and Controller?
- #class_inheritable_array :has_widgets_blocks
-
class << self
include WidgetShortcuts
-
- def has_widgets_blocks
- @has_widgets_blocks ||= []
- end
-
- def has_widgets(&block)
- has_widgets_blocks << block
- end
-
- # Use this for setup code you're calling in every state. Almost like a +before_filter+ except that it's
- # invoked after the initialization in #has_widgets.
- #
- # Example:
- #
- # class MouseWidget < Apotomo::Widget
- # after_initialize :setup_cheese
- #
- # # we need @cheese in every state:
- # def setup_cheese(*)
- # @cheese = Cheese.find @opts[:cheese_id]
- def after_initialize(method)
- self.initialize_hooks << method
- end
end
include TreeNode
include Onfire
include EventMethods
include Transition
include Caching
-
- include DeepLinkMethods
include WidgetShortcuts
helper Apotomo::Rails::ViewHelper
+
+
def add_has_widgets_blocks(*)
- self.class.has_widgets_blocks.each { |block| block.call(self) }
+ run_hook :has_widgets, self
end
after_initialize :add_has_widgets_blocks
# Constructor which needs a unique id for the widget and one or multiple start states.
@@ -87,17 +69,13 @@
@visible = true
@version = 0
@cell = self
- process_initialize_hooks(id, start_state, opts)
+ run_hook(:after_initialize, id, start_state, opts)
end
- def process_initialize_hooks(*args)
- self.class.initialize_hooks.each { |method| send(method, *args) }
- end
-
def last_state
@state_name
end
def visible?
@@ -209,19 +187,36 @@
render_view_for(options, @state_name) # defined in Cell::Base.
end
alias_method :emit, :render
-
+ # Wraps the rendered content in a replace statement targeted at your +Apotomo.js_framework+ setting.
+ # Use +:selector+ to change the selector.
+ #
+ # Example:
+ #
+ # Assuming you set
+ # Apotomo.js_framework = :jquery
+ #
+ # and call replace in a state
+ #
+ # replace :view => :squeak, :selector => "div#mouse"
+ # #=> "$(\"div#mouse\").replaceWith(\"<div id=\\\"mum\\\">squeak!<\\/div>\")"
def replace(options={})
content = render(options)
- Apotomo.js_generator.replace(self.name, content)
+ Apotomo.js_generator.replace(options[:selector] || self.name, content)
end
+ # Same as replace except that the content is wrapped in an update statement.
+ #
+ # Example for +:jquery+:
+ #
+ # update :view => :squeak
+ # #=> "$(\"mum\").html(\"<div id=\\\"mum\\\">squeak!<\\/div>\")"
def update(options={})
content = render(options)
- Apotomo.js_generator.update(self.name, content)
+ Apotomo.js_generator.update(options[:selector] || self.name, content)
end
# Force the FSM to go into <tt>state</tt>, regardless whether it's a valid
# transition or not.
### TODO: document the need for return.
@@ -297,7 +292,9 @@
end
def controller
root? ? @controller : root.controller
end
+
+ alias_method :widget_id, :name
end
-end
\ No newline at end of file
+end