Category: Flash/Actionscript
RubyAMF and AS2 (follow-up to AS3 + SSR, RubyAMF, and RESTful Rails)
A quick follow-up post to my tutorial on Flash and RubyAMF — RubyAMF can of course be used with AS2. Here's a short AS block showing a call to the index method of the PeopleController:
-
// remoting
-
import mx.remoting.Service;
-
import mx.remoting.PendingCall;
-
import mx.rpc.RelayResponder;
-
import mx.rpc.ResultEvent;
-
import mx.rpc.FaultEvent;
-
-
var peopleService:Service = new Service("http://localhost:3000/rubyamf/gateway", null, "PeopleController", null, null);
-
var peopleCall:PendingCall = peopleService.index();
-
peopleCall.responder = new RelayResponder(this, "onList", "onFault");
-
-
-
function onList(re: ResultEvent): Void
-
{
-
var people:Object = re.result;
-
for (var i = 0; i <people.length; i++) {
-
for (var j in people[i])
-
{
-
trace(j + ": " + people[i][j]);
-
}
-
}
-
}
-
-
function onFault(fault:FaultEvent): Void
-
{
-
trace("PeopleRest::onFault: " + fault);
-
}
Flash CS3 Components and WMode
I recently ran into an issue with the Flash CS3 ComboBox component – it doesn't render when you set the wmode for the movie to transparent. I was at a loss for a solution until I saw Sex Panzer's comment on this post.
Following his advice I added a stage.dispatchEvent(new Event(Event.RENDER)) line to my code, which basically kicks the code in the pants and gets it to render the component properly.
I tested a few other components and they all seem to be affected by the wmode bug, so hopefully the solution above works for all of them.
AS3 + SSR, RubyAMF, and RESTful Rails
Update 2008-05-02: This example was written using SSR v1 and RubyAMF 1.3.4, and does not currrently match what you get with the latest versions. I'll be updating it shortly to reflect the changes against both SSR and RubyAMF.
Update 2007-10-25: A quick AS2 example using the Rails controller detailed below can be found here.
The RubyAMF blog recently linked to a good tutorial on using Flex, RubyAMF, and RESTful Rails. At Domani we've been using RubyAMF for a few projects recently, but without Flex. So here's a quick tutorial on using Flash, SSR (Super Simple Remoting), and RubyAMF with RESTful Rails in a local development environment. This assumes the usage of OS X, Locomotive, and MAMP — for some notes on setting that up I wrote a quick post on setting that up here. As always, many thanks to Aaron and the crew behind RubyAMF for their work and quick responses to questions.
Set Up Your Database
Create a new database called people_development. I used the phpMyAdmin interface running on the MAMP instance to do this.
Next, create a new rails app in Locomotive (Applications > Create New). I called the app people. The first thing I usually do is bring up the info screen for the newly-created app and change the port from the default set by Locomotive. I've noticed that the listing in the main Locomotive window will still display the default port even after you change it; to avoid this hit tab from the port field while you're in the info screen and it should refresh the display in the main window.
Modify the database config file (config > database.yml) to add two things:
port: 8889
socket: /Applications/MAMP/tmp/mysql/mysql.sock
Also set the password to "root", which is the default for databases served by MAMP.
Select the Rails app in the main Locomotive listing, and hit Command+T to fire up a terminal session in the context of the app you created. From the terminal session run this command (one line):
ruby script/generate scaffold_resource person first_name:string last_name:string
Among other things, this will create a migration that we can run to create a table for our app in the database. From the terminal run:
rake db:migrate
At this point you should have people table in the people_development database.
Install RubyAMF
From your terminal session run (one line):
ruby script/plugin install http://rubyamf.googlecode.com/svn/trunk/rubyamf
After RubyAMF finishes the install, start the people app in Locomotive. Verify that the RubyAMF gateway is live by pointing your browser to http://localhost:3000/rubyamf/gateway.
Next, verify that the app is live by pointing your browser to http://localhost:3000/people. Create a few people to populate the database with some entries.
Add AMF format response
Add a line for the amf format response in the people controller file (app > controllers> people_controller.rb)
format.amf { render :amf => @people }
The index method should read:
-
def index
-
@people = Person.find(:all)
-
-
respond_to do |format|
-
format.html # index.rhtml
-
format.xml { render :xml => @people.to_xml }
-
format.amf { render :amf => @people }
-
end
-
end
Set Up Your Flash App
Create a new FLA called people_rest.fla. Drag a DataGrid component onto the stage so that the library contains the DataGrid symbol and the DataGrid classes can be. You can delete the DataGrid off the stage after this.
Download SSR from here. Unzip the folder, and move the org folder to the same location as your FLA.
Create a document class called PeopleRest.as, and link it to your FLA as the Document class via the Properties panel. PeopleRest.as will establish a connection to the Rails app via the RubyAMF gateway and make a call to the index method.
PeopleRest.as:
-
package {
-
import flash.net.Responder;
-
import flash.display.MovieClip;
-
import fl.controls.DataGrid;
-
import fl.data.DataProvider;
-
import flash.events.Event;
-
-
import org.rubyamf.remoting.ssr.*;
-
-
public class PeopleRest extends MovieClip
-
{
-
private var rs:RemotingService;
-
private var peopleGrid:DataGrid;
-
-
public function PeopleRest()
-
{
-
init();
-
}
-
-
private function init(): void
-
{
-
peopleGrid = new DataGrid();
-
peopleGrid.x = 50;
-
peopleGrid.y = 180;
-
peopleGrid.width = 400;
-
addChild(peopleGrid);
-
-
rs = new RemotingService("http://localhost:3000/rubyamf/gateway", "PeopleController");
-
rs.addEventListener(FaultEvent.CONNECTION_ERROR, onConnectFault);
-
rs.addHeader('recordset_format',false,'fl9');
-
rs.index([], onList, onFault);
-
}
-
-
private function onList(re:ResultEvent=null):void {
-
var people:Object = re.result;
-
var dp:DataProvider = new DataProvider(re.result);
-
peopleGrid.dataProvider = dp;
-
}
-
-
private function onFault(fault:FaultEvent):void
-
{
-
trace("PeopleRest::onFault: " + fault.fault.faultString);
-
}
-
-
private function onConnectFault(fe:FaultEvent):void
-
{
-
trace("PeopleRest::onConnectFault() " + fe);
-
}
-
-
};
-
-
}
Compile the movie. You should see the person entries in your database in the data grid component on stage.
Adding Create/Update/Destroy Functionality
At this point you can add functionality to PeopleRest.as by adding input fields and buttons for triggering calls to the create, update, and destroy methods in the controller. Below are the edits to the Rails controller that are needed to support AMF responses:
Create:
-
def create
-
if @is_amf
-
@person = Person.new();
-
@person.first_name = params[:first_name]
-
@person.last_name = params[:last_name]
-
else
-
@person = Person.new(params[:person])
-
end
-
-
respond_to do |format|
-
if @person.save
-
flash[:notice] = 'Person was successfully created.'
-
format.html { redirect_to person_url(@person) }
-
format.xml { head :created, :location => person_url(@person) }
-
format.amf { render :amf => @person }
-
else
-
format.html { render :action => "new" }
-
format.xml { render :xml => @person.errors.to_xml }
-
end
-
end
-
end
Update:
-
def update
-
@person = Person.find(params[:id])
-
-
if @is_amf
-
@person=Person.find(params[:id])
-
@person.first_name=params[:first_name]
-
@person.last_name=params[:last_name]
-
else
-
@event = Person.new(params[:person])
-
end
-
-
respond_to do |format|
-
if @person.update_attributes(params[:person])
-
flash[:notice] = 'Person was successfully updated.'
-
format.html { redirect_to person_url(@person) }
-
format.xml { head :ok }
-
format.amf { render :amf => @person }
-
else
-
format.html { render :action => "edit" }
-
format.xml { render :xml => @person.errors.to_xml }
-
end
-
end
-
end
Destroy:
-
def destroy
-
@person = Person.find(params[:id])
-
@person.destroy
-
-
respond_to do |format|
-
format.html { redirect_to people_url }
-
format.xml { head :ok }
-
format.amf { render :amf => 'deleted' }
-
end
-
end
The final FLA, Actionscript3 document class, and rails controller file are here: people_rest.zip.
Flash Forward, Day Three
Day three kicked off with a keynote by John Maeda. He spent the first part of his keynote giving background on his evolution as a digital artist, and the sources of inspiration for his work. He had a funny joke about his mom thinking Samsung was a person he was working with, Mr. Sam Sung. I'm about to read his latest book, The Laws of Simplicity.
The last third of the keynote was devoted to a presentation of E15, a new visualization environment for code/web content/somethingorother. Some details are here. Four of Maeda's grad students made the presentation.
I attended a couple of other sessions, including "AIR Out of the Box" by Keith Peters of Bit-101. That one was a lot of fun, demystifying the new AIR runtime as simply "Flash Player on Steroids" as well as decoupling the AIR development process from Flex.
FlashForward Boston, Day Two
In case you're wondering what happened on Day One, your guess is as good as mine. I'm playing catch-up to the FlashForward conference in Boston. I missed the first day to wrap up some development on a project, and flew out early this morning to fall into the jetstream of day two. Oscar got here yesterday, so he's been doing a lot of the networking and recruiting for Domani.
It's interesting going to one of these after missing the last few years. It's nice to see guys like Craig Swann continuing to use Flash as a way to creatively realize different forms of input (gestural, sonic, color, etc.). His presentation was full of different jumping-off points for experimentation, and hopefully I'll get a chance to play with some of the ideas he brought up. I know that some of the work the guys at Domani have been cooking up for the Dumbo Arts Festival is mining the same vein.
I expected the conference to be an all-out Adobe AIR-fest (and there's a lot of AIR-related content) but I can't shake the nagging feeling that the general Flash community hasn't quite figured out what it wants to start using AIR for. It's similar to the stance that many had with Flex, where they had to figure out where the new technology might fit into their development workflow.
On a personal note I'm crashing with Joseph and Kristen at their new digs in Brighton. It's always fun to see where your family lives, to form a sense of place that your mind can use to situate them visually.







