HomeAbout Mark van HedelContact Mark van Hedel



Using FLEX for input forms in BlogCFC :: Part 3

Now that we did the first two parts let's continue. First we created a form in FLEX and retrieved all strings we would need for the application. After that we retrieved the user information in case the user has chosen to remember it's values for the site. Next we want the title for the subject, so we can also display that in the form. We could have done that in Actionscript as well, but in this case I've chosen to retrieve that from the server using a remote object connection again.

So for this I just added a simple method in my CFC to return the entry I want, passing in the ID of the item I want to add commments to.

<cffunction name="retrieveEntry" displayname="retrieveEntry" hint="Retrieve current Entry" access="remote" output="false" returntype="struct">
      <cfargument name="entryID" type="String" required="true" displayname="entryID">
      <cfreturn application.blog.getEntry(arguments.entryID,true)>
   </cffunction>
Now from FLEX we can call this method just like we did before with the resource bundle at creationComplete. Since we don't want the same handler handling all our calls to our Remote Objects independent of what method we call I created another responder class to handle this call and work with the results. This responder class looks like this :
package org.flexpair.blog.responders
{
   import mx.controls.Alert;
   import mx.rpc.IResponder;
   import mx.rpc.events.FaultEvent;
   import mx.rpc.events.ResultEvent;
   
   import org.flexpair.blog.vo.EntryVo;

   public class EntryResponder implements IResponder
   {
      [Bindable]
      public var entry:EntryVo;
      /**
       * Handle the result of retrieving entry data from the server
       */
      public function result(data:Object):void
      {
         // Fill the object with values
         var re:ResultEvent = data as ResultEvent;
         entry.allowComments = re.result.ALLOWCOMMENTS;
         entry.title = re.result.TITLE;
      }

      /**
       * Handle any fault in the retrieval of entry data
       */
      public function fault(info:Object):void
      {
         //TODO: implement function
         var fault:FaultEvent = info as FaultEvent;
         Alert.show(fault.message.toString());
      }
      
   }
}

As you might see in the responder we are going to update another Value Object with the values we get back, this Value Object doesn't contain the entire post but just the values we need, so in this case I made this Value Object very simple with the next few properties.

package org.flexpair.blog.vo
{
   /**
    * Class to hold entr values
    */
   [Bindable]
   public class EntryVo
   {
      public var allowComments:Boolean;
      public var title:String = "";
      public var id:String;
   }
}

Now finally we can make the call and populate the last set of values from the server, so as I did with the Resource Bundle I create an instance of the Value Object first and use that to fill my default values in my form, and pass on that instance to my responder to be updated as soon as the information comes in. Because it's all working with a binding this will directly update my form and display the information. One reminder from this point on, your application will give you an error when you run it directly from flexBuilder, this since there is no ID known in the HTML page you run it from, so if you want to prevent that from happening, just change your HTML page, that is being used to return values that don't give errors for your website. So you might look in the html-template folder and in there change the index file to have the javascript methods and return something from there, since this is not the file we are going to use in the real application it will not have any effects if you change it. Updated creationCompleteHandler :

// Responders
private var _rbResponder:ResourceBundleResponder;
private var _entryResponder:EntryResponder;

// resource bundle values
[Bindable]
private var _rbValues:ResourceBundleVo = new ResourceBundleVo();

// entry values
[Bindable]
private var _entry:EntryVo = new EntryVo();

/**
* Handle creation complete event to retrieve data and populate the form.
*/
private function creationCompleteHandler():void {
   // Retrieve resource bundle
   _rbResponder = new ResourceBundleResponder();
   _rbResponder.rbValues = _rbValues;
   var token:AsyncToken = remoteUpdate.retrieveRbValues("addComments");
   token.addResponder(_rbResponder);
   
   // Retrieve user settings
   fillComments();
   _entry.id = ExternalInterface.call("getID");

   // Retrieve entry
   _entryResponder = new EntryResponder();
   _entryResponder.entry = _entry;
   var tkn:AsyncToken = remoteUpdate.retrieveEntry(_entry.id);
   tkn.addResponder(_entryResponder);
}

Now that we have retrieved that part of the information the form will be filled with every bit of info that is needed to make it possible for someone to fill in the comments.

After this part there are a few more steps to go that I'll get into later

  • Validating form input and cancelling the input
  • Submitting the form to the server
  • Remembering user data if the user wants to be remembered

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner