<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="srv.getElements(0,20000)">
  
  <mx:Script>
  <![CDATA[
  import mx.collections.ArrayCollection;
  import mx.events.CollectionEvent;
  import mx.events.CollectionEventKind;
  
  [Bindable] private var ffCount:Number = 0;
  [Bindable] private var ffTime:Number = 0;
  [Bindable] private var d:ArrayCollection;
  private var oneStepDownSearchText:String = "";
  private var ffStartDate:Date;
  private var _ffCount:Number = 0;
  
  private function ff(item:Object):Boolean
  {
    if ((oneStepDownSearchText == "") || (item["__includedInFilter__" + oneStepDownSearchText] != null))
    {
      _ffCount++;
      
      // do real test
      if ((item.id.toString()).indexOf(filterText.text) == 0)
      {
        item["__includedInFilter__" + filterText.text] = true;
        
        return true;
      }
    }
    
    return false;
  }
  ]]>
  </mx:Script>
  
  <mx:AMFChannel id="myamf" uri="http://www.jamesward.com/census/messagebroker/amf"/>
  
  <mx:ChannelSet id="channelSet" channels="{[myamf]}"/>
  
  <mx:RemoteObject id="srv" destination="census-ro" showBusyCursor="true" channelSet="{channelSet}" endpoint="/census/messagebroker/amf?gzip=true">
    <mx:result>
    d = new ArrayCollection(event.result as Array);
    d.filterFunction = ff;
    d.addEventListener(CollectionEvent.COLLECTION_CHANGE, function(event:CollectionEvent):void {
      if (event.kind == CollectionEventKind.REFRESH)
      {
        ffTime = ((new Date()).time - ffStartDate.time);
        ffCount = _ffCount;  // update the bindable property once
      }
    });
    </mx:result>
  </mx:RemoteObject>
  
  <mx:FormItem label="Search the based on id (numbers):">
    <mx:TextInput id="filterText" restrict="0-9">
      <mx:change>
      _ffCount = 0;
      
      if (filterText.text.length > 0)
      {
        oneStepDownSearchText = filterText.text.substr(0, -1);
      }
      else
      {
        oneStepDownSearchText = "";
      }
      
      ffStartDate = new Date();
      d.refresh();
      </mx:change>
    </mx:TextInput>
  </mx:FormItem>
  
  <mx:DataGrid dataProvider="{d}"/>
  
  <mx:Label text="ff was called {ffCount} times"/>
  
  <mx:Label text="filter took {ffTime} ms"/>
  
</mx:Application>