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!