Counter
After being compared to seaside I had a look at this project. Looks great, it is a stateful component framework in Smalltalk. To demonstrate my concepts in Apotomo I had to steal the counter example from the page.
As usual, we implement the counter logic in a cell.
app/cells/counter_cell.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class CounterCell < Apotomo::StatefulWidget def transition_map { :_increase => [:_increase, :_decrease], :_decrease => [:_increase, :_decrease], } end def count @count = 0 jump_to_state :_increase end def _increase @count += 1 state_view :_count end def _decrease @count -= 1 state_view :_count end end |
The widget is plugged into an arbitrary part of the GUI.
app/apotomo/application_widget_tree.rb
some_pg << cell(:counter, :count, 'my_counter')
Start state is :count, which initializes the counter and jumps to the state :_increment (line 10-11). Here we actually increment our counter and render the view _count (line 16). Let’s look at the view.
app/cells/counter/_count.html.erb
<h1><%= @count %></h1>
<%= link_to_event("--", :state => :_decrease) %>
<%= link_to_event("++", :state => :_increase) %>Simple stuff. We create two links which reference to the current widget my_counter and push the widget state machine into the respective state. Looking at the transition map (line 4-5) we see that the requested states are permitted.
After all this coding, I have to admit that the comparison with seaside made me somewhat proud.
Tags: transition_map

