Apotomo is a stateful widget component framework for Rails.
A stateful WHAT?
Well, you know that. In Rails, people tend to have fat controllers. One controller action renders the complete page. While many programmers try to separate their code into helpers, controller actions, RJS logic and partials, it is still the controller that has to care about when to update what, and how!
Man, gimme code!
Let's use the famous and tiresome counter example.
class CounterCell < Apotomo::StatefulWidget transition :from => :display, :to => :increment def display respond_to_event :counterClick, :with => :increment @count = 0 render # renders display.html.erb end def increment @count += 1 # @count is simply there - that's stateful. render :view => :display end end
Since this widget calls render it surely needs a view.
<!-- I'm display.html.erb --> <h1><%= @count %></h1> <%= link_to_event "Increment me!", :counterClick %>
We now plug the widget in a page.
class ExistingController < ApplicationController include Apotomo::ControllerHelper def some_action # do what you want... use_widgets do |root| root << cell(:counter, :display, 'my_first_counter') end end end
As soon as the widget is rendered it will jump to its :display state which initializes the counter and renders itself.
Speaking of rendering: how do we place the widget in our controller?
<!-- I'm some_action.html.erb --> <p> <%= render_widget 'my_first_counter' %>
Ok, so this renders the widget in our controller page.
When clicking the link it updates automatically on the screen showing the incremented value. Wow.
Yes, it is.
Is there more?
Apotomo got a load of features.
Widgets can range from small standalone components to nested widget trees like dashboards or forms. Remember that each widget can have any number of children.
- Bubbling events
Widgets can trigger events and watch out for them. While events bubble up from their triggering source to root they can be observed, providing a way to implement loosely coupled, distributable components.
- Deep Linking
Apotomo deals with deep links (or url fragments) out-of-the-box while using SWFAddress. Components that register for deep linking will update as soon as the deep link changes. That makes your application back-button-safe!
Needless to say that it is simply easier to test small components instead of fat do-it-all controllers.
Give it a try- you will love the power and simplicity of real stateful components!
Copyright © 2007-2010 Nick Sutterer <firstname.lastname@example.org>
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.