Fork me on GitHub

swap

An article by Gaspard Bucher

Changes attribute values from a list. Must be used inside an ajaxable context (group, each).

Changes attribute values from a list. Must be used inside an ajaxable context (group, each).

  • publish Set whether to auto-publish node after save.
  • states list of states the swap should loop in.
  • attr attribute to change.
  • update remote block to update (can be ‘_page’)
see publish

The ‘swap’ method is used to loop through a list of values by repeatedly clicking on a link.

In this first example, the ajax call replaces the whole “each” block after ‘flag’ value changes.

<ul do='tasks from project'>
  <li do='each' class='#{flag}'>
    <r:swap attr='flag' states='todo,done,alert'/><r:link/>
  </li>
</ul>

When the ‘update’ argument is omitted, the closest block or each loop is used.

In this second example, only the defined “block” is changed.

<ul do='tasks from project'>
  <li do='each'>
    <span do='block' class='#{flag}'>
      <r:swap attr='flag' states='todo,done,alert'/><r:flag/></span><r:link/>
  </li>
</ul>

This example uses the integer field “custom_a” and the states includes the nil value.

<div id='swap' class='orange' do='block' green_if='custom_a == 100'>
  <span do='swap' states='100, ' attr='status' publish='true' do='t("img_status#{status}")'/> <r:title/>
</div>

advanced

In case you want to change more then one argument or need other settings, you can use pure rubyless to achieve the same result with the “next_in_list” method and setting ‘update’ as link parameter:

<r:link action='update' update='_page' node[flag]='#{next_in_list(flag, "todo,done")}'/>