Example: Save and Load Comments with Adobe Flex

Last modified: October 13, 2007 - 05:34

This code sample demonstrates how to use the commenting methods of the Services module via Adobe Flex.

First ensure you have Drupal installed with the Services and AMFPHP modules properly set up. Refer to this page for more details on how to do this.
To add the commenting methods to the Services module, apply this patch: http://drupal.org/node/182069

Next copy the following MXML code into a new Flex Project and then update the endpoint to point to your AMFPHP gateway:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="405" width="338">
<mx:Script>
<![CDATA[
import mx.rpc.events.*;

[Bindable]
public var comments:Array;

public function loadComments():void{

var requestedFields:Array = new Array();
requestedFields[0] = 'cid';
requestedFields[4] = 'comment';

// requestedFields is an optional parameter. 
// If it isn't specified then all fields will be returned.
comment.getNodeComments(loadNID.text, requestedFields);
}

public function saveComment():void{

var newComment:Object = new Object;

// required fields
newComment.nid = saveNID.text;
newComment.comment = body.text;

comment.saveComment(newComment);
}

public function loadCommentsResult(event:ResultEvent):void{
comments = event.result as Array;
}

public function saveCommentResult(event:ResultEvent):void {
mx.controls.Alert.show("Comment Saved");
}

public function onFault(event:FaultEvent):void{
mx.controls.Alert.show(event.fault.faultString, "Error");
}

]]>
</mx:Script>

<mx:RemoteObject endpoint="http://localhost/drupal/services/amfphp" showBusyCursor="true" destination="amfphp" source="comment" id="comment">
<mx:method name="getNodeComments" result="loadCommentsResult(event)" fault="onFault(event)"/>
<mx:method name="saveComment" result="saveCommentResult(event)" fault="onFault(event)"/>
</mx:RemoteObject>

<mx:DataGrid x="10" y="259" width="318" id="nodeComments" dataProvider="{comments}" height="135" />

<mx:Label x="10" y="12" text="Add Comment" fontWeight="bold"/>
<mx:Label x="10" y="191" text="Load Comments" fontWeight="bold"/>
<mx:Label x="10" y="233" text="Node ID:"/>
<mx:Label x="10" y="41" text="Node ID:"/>
<mx:Label x="10" y="69" text="Body:"/>

<mx:Button x="90" y="152" label="Save Comment" click="saveComment()"/>
<mx:Button x="110" y="231" label="Load Comments" width="108" click="loadComments()"/>

<mx:TextArea x="68" y="68" width="159" height="76" id="body"/>
<mx:TextInput x="68" y="39" width="34" id="saveNID"/>
<mx:TextInput x="68" y="231" width="34" id="loadNID"/>

</mx:Application>

Finally, run the Flex application. Try saving a new comment by choosing a node id and typing your comment in the body field. After the comment was saved successfully try loading the comments of that node.

If you run into problems you should check that you have the proper commenting permissions set up in Administer - User Management - Access Control.

getting this working with 5

scarer - July 31, 2008 - 04:30

I don't know whether it's a version thing or not but I ended up changing the line comment.saveComment to comment.save.

Cheers,

Sarah

 
 

Drupal is a registered trademark of Dries Buytaert.