Jun 19 2007
Lightweight server push with Rails and AIR
Alex MacCaw has posted a really nice example of how his Juggernaut plugin for server-side push can be used with Adobe’s AIR (formerly Apollo) runtime to create a simple chat application.
Jun 19 2007
Alex MacCaw has posted a really nice example of how his Juggernaut plugin for server-side push can be used with Adobe’s AIR (formerly Apollo) runtime to create a simple chat application.
Jun 19 2007
In a previous post I discussed how you can use the include statement to ‘mixin’ methods and properties defined in an ActionScript file into your classes. I find this approach to be somewhat limited, so I’ve put together a class that allows you to mix one class into another. The class is an all-static class that is simply called Mixin. The class is a bit long to include in this post, but you can view the source by clicking here.
Here is a quick example of how you can use this class. Let’s start with two classes that we will use as mixins:
public dynamic class ClassMixin { import flash.utils.*; // bound method that retains reference to this class // this will always return the fully qualified path // to this class public function whoAmI () : String { return getQualifiedClassName(this); } // function expression that is attached to the prototype // this return the class name of the class that it is // called from prototype.whoBeMe = function () : String { return getQualifiedClassName(this); }; // this will return the class object for the instance // that invokes it prototype.myClass = function () : Class { return getDefinitionByName(this.whoBeMe()) as Class; }; }
public class Ext { public static var staticVar : Number = 123; public var instanceVar : Number = 321; public static function staticMethod () : String { return 'I am static'; } public function instanceMethod () : String { return 'I am an instance'; } public function iCallProtected () : String { return iAmProtected(); } protected function iAmProtected () : String { return 'Protected'; } }
And a third class that is the mixee (the one that we will mix the other two classes into).
public dynamic class Model { // dynamic reference to 'this' class object. private static var self : Class = prototype.constructor; // mixin classes during static init Mixin.extendClass(self, ClassMixin); Mixin.extendClass(self, Ext); public function Model() { } }
In the Model class above you will see two calls to Mixin.extendClass(). The extendClass method takes the class in the second argument and mixes its methods and properties (both static and instance) in to the class in the first argument. These extendClass method calls are made directly inside the class block, so they are invoked when the Model class object is initialized. You can read more about this in my previous post about static initializations in ActionScript.
So, now if you were to execute the following class:
public class Main extends Sprite { public function Main() { var m : Model = new Model(); //bound method returns original class name trace (m.whoAmI()); // this function is not bound and it returns name // of the class that is calling the function trace (m.whoBeMe()); trace(m.myClass()); //mixed in from Ext trace (Model['staticMethod']()); trace (m.instanceMethod()); trace(m.myClass().staticVar); trace(m.instanceVar); // mixed in method calls a protected method in the Ext class trace (m.iCallProtected()); // you can override methods that are attached to prototype at runtime Model.prototype.whoBeMe = function () : String { return 'I am you'; }; trace (m.whoBeMe()); } }
You would see the following in the console:
com.rxr.mixinexample::ClassMixin
com.rxr.mixinexample::Model
[class Model]
I am static
I am an instance
123
321
Protected
I am you
You can view the full source and download it here.
Jun 15 2007
In a previous post I discussed how you can use the [Mixin] metadata tag to create a static initialization in Flex. Here is another way to do pretty much the same thing:
public dynamic class Model { trace('I get called first'); public function Model() { } }
The trace statement and any other code that is directly inside the class body will be invoked when the class is loaded. You can read more about this here.
Jun 14 2007
If you are interested and in Flex and Rails, and you know french, then be sure to check out this tutorial by Laurent Bois.
Jun 13 2007
redMine is a very nice, open-source (GPL), project management application that is being built with Ruby on Rails. If you are thinking about using Trac for project managment, then I definitely recommend that you take a look at redMine. I found that redMine was quite a bit easier to install than Trac, and I also found that it was easier to manage multiple projects with redMine. Also, I just generally like redMine’s interface.
Jun 13 2007
In a two part interview on the Flex Show, Tony Hillerson of effectiveUI discusses the benefits of working with Rails and why you might want to use Rails as the back-end for your Flex apps.
Jun 13 2007
That title may be a bit of a stretch, AS3 does not support mixins in the same way as Ruby, but it is possible to define a set of methods and/or attributes in a single file and then mix those methods and attributes into your AS3 classes. Here’s how:
First create a new ActionScript file (a file, not a class) then add the methods and/or attributes that you want to mix in to your classes.
For example, I created a file called classmixin.as that looks like this:
// ActionScript file import flash.utils.*; // returns the name of the class public function whoAmI () : String { return getQualifiedClassName(this); } // returns the class object public function myClass () : Class { return getDefinitionByName(whoAmI()) as Class; } // returns the prototype object public function proto () : Object { return myClass().prototype; }
Now, let’s say that you want to add these methods directly to two classes, one is controller called ContactsController and one is a model called Contact. To do this you need to use the include statement along with the relative path to file that defines the methods. For example:
package com.rxr.mixinexample.control { public class ContactsController { include "/../classmixin/classmixin.as" } }
package com.rxr.mixinexample.model { public class Contact { include "/../classmixin/classmixin.as" } }
That’s it. Now both of these classes have all of the methods defined in classmixin.as, so if you were to run the following code:
package { import flash.display.Sprite; import com.rxr.mixinexample.control.ContactsController; import com.rxr.mixinexample.model.Contact; public class MixinExample extends Sprite { public function MixinExample() { var a : ContactsController = new ContactsController(); var b : Contact = new Contact(); trace(a.whoAmI()); trace(b.whoAmI()); } } }
you would see these trace statements in the console:
com.rxr.mixinexample.control::ContactsController
com.rxr.mixinexample.model::Contact
This mixin capability in AS3 works a bit differently than mixins in Ruby. In Ruby when you mixin a module you are really creating a reference to that module and when you call a method that was mixed in it is actually being delegated to the module. Also, in Ruby when you mixin in a module only the member methods get mixed in, though there is way to mixin statics as well, but you have to add some extra code to make this happen. This is because the module is an object itself and the static methods belong to the module, so if you want to add them to your class you need to extend the module.
In AS3 when you include a .as file, it is though you are appending that file directly to the class definition. So you are not creating a reference to a module, you are just adding the contents of the file to your class, so if you define static attributes or methods they will be added to your class along with the member attributes and methods.
Please let me know if you have any questions, or if any of this is unclear.