Example: Rating a Node with Adobe Flex

Last updated on
30 April 2025

This code sample demonstrates how to use the vote up down methods of the Services module via 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.

You must have the vote_up_down module installed in order to enable the vote_up_down service. First install the vote_up_down module and then apply this patch to the Services module to reveal the new services methods (see also http://drupal.org/node/241453, this feature seems to be in flux).

Before trying to access the service via Flex, ensure you have the proper settings and permissions of the vote_up_down module set up so that you can vote through the normal Drupal interface.

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="171" width="318">
	<mx:Script>
		<![CDATA[
		
			import mx.rpc.events.*;
					
			public function onFault(event:FaultEvent):void{
				mx.controls.Alert.show(event.fault.faultString, "Error");
			}
			
			public function onSubmitResult(event:ResultEvent):void{
				mx.controls.Alert.show("Submit status (seems to be false on success): " + event.result);
			}
			
			public function onResultResult(event:ResultEvent):void {
				mx.controls.Alert.show("Vote sum = " + event.result,"Results");
			}
			
			public function submitVote():void{
				vote_up_down.submitVote("node", nid.text, voteValue.text);	
			}
			
			public function viewResults():void{
				vote_up_down.getVoteResult("node", nid.text);
			}
			
		]]>
	</mx:Script>
		
	<mx:RemoteObject endpoint="http://localhost/drupal/services/amfphp" showBusyCursor="true" destination="amfphp" source="vote_up_down" id="vote_up_down">
		<mx:method name="submitVote"		result="onSubmitResult(event)" 		fault="onFault(event)" />
		<mx:method name="getVoteResult" 	result="onResultResult(event)" 		fault="onFault(event)" />
	</mx:RemoteObject>
	
	<mx:Label x="10" y="10" text="Vote Up Down Services" fontWeight="bold"/>
	<mx:Label x="10" y="37" text="Node ID:"/>
	<mx:Label x="10" y="63" text="Vote:"/>
	<mx:Label x="160" y="65" text="1 or -1 to vote up or down"/>
	<mx:TextInput x="98" y="36" width="54" height="19" id="nid" text="11"/>
	<mx:TextInput x="98" y="63" width="54" height="19" id="voteValue" text="1"/>
	<mx:Button x="98" y="101" label="Vote" click="submitVote()" width="54"/>
	<mx:Button x="54" y="133" label="View Results" click="viewResults()"/>
	
</mx:Application>

Finally run the application and test it by entering a node id and submitting a +1 or -1 vote. After the vote is cast click "View Results" to check that your vote was counted.

Help improve this page

Page status: Not set

You can: