malkomalko
3/12/2009 - 10:16 PM

gistfile1.rb

Flex/Actionscript Side

Item.as:

...
public class Item extends RxTreeModel {
  public static var sortString:String = '';
  public static const LABEL:String = "whatever";

...
public function Item() {
  sortString == 'id' ? super('id') : super(LABEL);
}

Component.mxml:

...

[Bindable] public var selectedTile:Object;

private function draged(event:DragEvent):void {
  // sets the sortString variable to 'id' so that the dataProvider shows your ids
  Item.sortString = 'id';

  // Rx.http().invoke() is used to call non-Restful actions on your controller
  // "items/reorder" tells it to go to the reorder action of the items controller
  // {id: val, order: next_val} is passing paramaters
  // you call can call these in your ruby controller action via params[:id] and params[:order]
  Rx.http().invoke("items/reorder", {id: selectedTile.id, order: idOfTileList.dataProvider});
}

...

<TileList id="idOfTileList" itemRenderer="if.you.want.a.TileListRenderer"
  dragComplete="draged(event)" change="{selectedTile = idOfTileList.selectedItem}"
  width="100%" height="100%" columnWidth="270" rowHeight="135"
  dragEnabled="true" dragMoveEnabled="true" dropEnabled="true"/>
routes.rb:

# if you are adding additional actions onto a restful resource, you define a collection method
map.resources :items, :collection => {:reorder => :get}

item.rb:

class Item < ActiveRecord::Base
  acts_as_list
end

boards_controller.rb:

...
def reorder
  # first get the Item that you are moving
  @item = Item.find(params[:id])

  # this is how we get the dropped index of the item
  # first we use the split method to convert the string into an array
  # next we find the index of the current item
  # we convert that string to an integer and add 1 since ruby array's are 0 based
  @position = params[:order].split(',').index(params[:id]).to_i + 1

  # acts_as_list method, does the rest of the magic for you
  @item.insert_at @position

  # render nothing so you don't get MissingTemplate errors
  render :nothing => :true
end
...