Apotomo 1.1.1 Released – Simpler Bubbling Events!

{{{
We released a new version for Apotomo, the widget framework for Rails. Here’s a brief overview what “1.1.1”:https://github.com/apotonick/apotomo gives us.

h3. What’s new?

* *Inheritance for @#responds_to_event@* Observers are now inherited, which makes it simpler to work with inheritance.
* *Easier global observers* @#responds_to_event@ got a new option to simplify attaching observers to root (or any other widget).

h3. Inheritance for observers

Say we had a widget for listing todo items.

class TodosWidget  :update
  
  def display
    @todos = Todo.find(:all)
    render
  end

  def update(evt)
    replace :state => :display
  end
end

As soon as a @:newTodo@ event is met the widget will redraw.

h3. Widgets and OOP

Now if we need another widget *showing completed todos*, only, we can use inheritance.

class CompletedTodosWidget < TodosWidget
  def display
    @todos = Todo.completed
    render
  end
end

All we have to overwrite is the @#display@ state to grab completed todos. Everything else
* the @responds_to_event@ observer
* the remaining state methods
* and the views
are *_inherited_* from @TodosWidget@!

In former versions, you had to _redefine_ the observers in the derived class, which was the opposite of cool.

h3. Simple global observers

Often, there are widgets on the same tree level.

class DashboardController < ApplicationController

  has_widgets do |root|
    root << widget(:todos)
    root << widget(:todo_form)

Both widgets are attached to @root@ – they’re hierarchically on the same level.

The @todo_form@ widget triggers an event when there’s a new submission.

class TodoFormWidget  :process
  
  def process(evt)
    # save the new todo
    trigger :newTodo
  end

The event will bubble from @todo_form@ to @root@ – how could the @todos@ widget observe that event in order to update?

h3. A new option for @responds_to_event@

Well, attach the observer to @root@ – you will surely catch every event there (except if you @stop!@ it somewhere),

class TodosWidget  :update,
    :passing => :root

The @:passing@ option does exactly that. As soon as the @:newTodo@ event is encountered in @root@, it will trigger the @TodosWidget#update@ state. It’s so simple.

Note that you can attach observers to _any_ widget in the tree using @:passing@.

h3. Go for it!

Let us know how those tiny changes work for you. Cheers!

}}}

Advertisement

One thought on “Apotomo 1.1.1 Released – Simpler Bubbling Events!

  1. This is a great addition to Apotomo. I use the :passing option all the time when a parent widget triggers an event that its children need to respond_to. (In my case, the parent processes events and the children have to be updated.) Thanks, Nick!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s