Skip to content

Instantly share code, notes, and snippets.

@hiddentrak
Created February 15, 2011 14:01
Show Gist options
  • Save hiddentrak/827557 to your computer and use it in GitHub Desktop.
Save hiddentrak/827557 to your computer and use it in GitHub Desktop.
Controller for calling Flashpress services
package com.hiddentrak.site.wp
{
import flash.events.EventDispatcher;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.net.Responder;
import flash.net.registerClassAlias;
import flash.system.Security;
import flashpress.vo.CaseStudySectionVO;
import flashpress.vo.CaseStudyVO;
import flashpress.vo.NextGenGalleryVO;
import flashpress.vo.WpAttachmentVO;
import flashpress.vo.WpBlogInfoVO;
import flashpress.vo.WpBlogRollVO;
import flashpress.vo.WpCommentVO;
import flashpress.vo.WpMetaVO;
import flashpress.vo.WpPostCountVO;
import flashpress.vo.WpPostVO;
import flashpress.vo.WpSearchVO;
import flashpress.vo.WpTermVO;
public class WPController extends EventDispatcher
{
protected var _connection : NetConnection;
private static var _instance : WPController;
private static var SERVICE : String = "flashpress.WpMethodsService.";
public function WPController( e : enforcer )
{
//need to registerClassAlias for amf class serialization to work in Flash
registerClassAlias("flashpress.vo.WpPostVO", WpPostVO);
registerClassAlias("flashpress.vo.WpAttachmentVO", WpAttachmentVO);
registerClassAlias("flashpress.vo.WpBlogInfoVO", WpBlogInfoVO);
registerClassAlias("flashpress.vo.WpBlogRollVO", WpBlogRollVO);
registerClassAlias("flashpress.vo.WpTermVO", WpTermVO);
registerClassAlias("flashpress.vo.WpMetaVO", WpMetaVO);
registerClassAlias("flashpress.vo.WpCommentVO", WpCommentVO);
registerClassAlias("flashpress.vo.WpPostCountVO", WpPostCountVO);
registerClassAlias("flashpress.vo.WpSearchVO", WpSearchVO);
registerClassAlias("flashpress.vo.NextGenGalleryVO", NextGenGalleryVO );
registerClassAlias("flashpress.vo.CaseStudyVO", CaseStudyVO );
registerClassAlias("flashpress.vo.CaseStudySectionVO", CaseStudySectionVO );
}
public static function getInstance():WPController
{
if( !_instance )
{
_instance = new WPController( new enforcer() );
}
return _instance;
}
public function connect( gateway : String = null ):void
{
Security.allowDomain( "http://yourdomain.com" );
gateway = "http://yourdomain.com/amfphp_path/gateway.php";
_connection = new NetConnection();
_connection.addEventListener( NetStatusEvent.NET_STATUS, onNetStatus );
_connection.connect( gateway );
}
public function callService( serviceFunction : String, resultHandler:Function, faultHandler:Function, ...args ):void
{
// Create responder
var responder:Responder = new Responder(resultHandler, faultHandler);
// Create an array that will temporarily store all the arguments
var collectArgs:Array = new Array;
// Add the fixed arguments
collectArgs.push((SERVICE + serviceFunction));
collectArgs.push(responder);
// Loop through the optional arguments and add them too
for (var i:uint=0; i<args.length; i++){
collectArgs.push(args[i]);
}
// Create a reference to the function we will call
var callFunction:Function = _connection.call;
// Call the function using the arguments
callFunction.apply(_connection,collectArgs);
}
protected function onFault( e : * ):void
{
trace( "FAULT : " + e );
}
protected function onNetStatus( event : NetStatusEvent ):void
{
trace( "NET CONNECTION SUCCESS" );
}
}
}
internal class enforcer{}
import com.gaiaframework.api.*;
import com.taurus.site.wp.WPController;
import flashpress.vo.WpPostVO;
WPController.getInstance().connect();
WPController.getInstance().callService( "getPostorPageByTitle", onPage, onFault, Gaia.api.getDeeplink()[0] );
protected function onPage( page : WpPostVO ):void
{
title_txt.htmlText = page.post_title;
content_txt.htmlText = page.post_content;
}
protected function onFault( fault : * ):void
{
trace( "[SERVICE FAULT] : " + fault );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment