Tony Hillerson presented on using Flex and Rails together. He discussed using XML, JSON and AMF for communication between the two frameworks. AMF provides the best integration as it handles data types automatically. The workflow for developing with Flex and Rails using AMF involves generating models, migrating the database, adding sample data, testing, configuring AMF mappings, wiring services, and repeating. Future improvements may include developing AMF as a gem and C extension. Resources for learning more are provided.
19. Con guring RubyAMF
ClassMappings.assume_types = true
class Context < ActiveRecord::Base
matches
[RemoteClass(alias=quot;Contextquot;)]
public class Context {
for free
20. Con guring RubyAMF
ClassMappings.assume_types = false
class Context < ActiveRecord::Base
matches
[RemoteClass(alias=quot;Contextquot;)]
public class Context {
with registration
ClassMappings.register(
:actionscript => 'Context',
:ruby => 'Context',
:type => 'active_record',
:attributes => [quot;idquot;, ...])
21. Con guring RubyAMF
ParameterMappings.scaffolding = false
def save
becomes
save(context); @context = params[0]
ParameterMappings.scaffolding = true
def save
save(
@context =
becomes
{context:context}
params[:context]
);
22. Connecting to Rails via
RubyAMF
<mx:RemoteObject
id=quot;contextsServicequot;
destination=quot;rubyamfquot;
endpoint=quot;http://localhost:3000/rubyamf_gateway/quot;
source=quot;ContextsControllerquot;
showBusyCursor=quot;truequot;
/>
public function save(context:Context):void {
var call:AsyncToken =
contextsService.save({context:context});
call.addResponder(responder);
}
24. 1. Generate and Migrate
$ script/generate rubyamf_scaffold context label:string
class CreateContexts < ActiveRecord::Migration
def self.up
create_table :contexts do |t|
t.string :label
t.timestamps
end
end
def self.down
drop_table :contexts
end
end
$ rake db:migrate
25. def load_all
@contexts = Context.find :all
respond_to do |format|
format.amf { render :amf => @contexts }
end
end
def save
respond_to do |format|
format.amf do
if params[:context].save
render :amf => params[:context]
else
render :amf =>
FaultObject.new(params[:context].errors.join(quot;nquot;))
end
end
end
end
26. 2. Sample Data
work:
id: 1
label: Work
home:
id: 2
label: Home
anarco_syndicalist_commune_biweekly_meetings:
id: 3
label: Anarcho-syndicalist Commune Bi-weekly Meetings
27. 3. Test
class Context < ActiveRecord::Base
validates_presence_of :label
class ContextTest < ActiveSupport::TestCase
def test_context_without_label_fails
non_label_context = Context.new
assert !(non_label_context.save)
error_messages = non_label_context.errors.on(:label)
assert !(error_messages.empty?)
end
30. 5. Wire
public function loadAll():void {
var call:AsyncToken = service.load_all();
call.addResponder(responder);
}
public function save(context:Context):void {
var call:AsyncToken =
service.save({context:context});
call.addResponder(responder);
}
public function destroy(context:Context):void {
var call:AsyncToken =
service.destroy({id:context.id});
call.addResponder(responder);
}
31. 5. Wire
public function execute(event:CairngormEvent):void {
var evt:SaveContextEvent = event as SaveContextEvent;
var delegate:ContextsDelegate = new ContextsDelegate(this);
delegate.save(evt.context);
}
public function result(data:Object):void {
var result:ResultEvent = data as ResultEvent;
var context:Context = result.result as Context;
...
}
39. Your
Bob
Father
You
Thanks!
Tony Hillerson
http://slideshare.com/thillerson
http://github.com/thillerson
http://thillerson.blogspot.com
http://effectiveui.com
Twitter: thillerson
Brightkite: thillerson
39