Community Documentation

Example: Save and Load Comments with Adobe Flex

Last updated April 6, 2010. Created by wavelan on October 13, 2007.
Edited by heyrocker. Log in to edit this page.

NOTE: Commenting services were integrated into Services 6.2, but are not reflected in this example. Some calls may need to be changed, and the linked patch may not work properly.

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.

Comments

getting this working with 5

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

Page status

Needs updating

Log in to edit this page

About this page

Drupal version
Drupal 5.x

Develop for Drupal

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.