Feb 5

Strict typing is good mkay

Posted by Cliff

I’m currently in the final stages of a refactor and I thought I’d just share this little tidbit on why strict typing is a good thing in AS3, especially when working in an agile environment where things can change quickly as requirements unfold and mutate.

Let’s say you’re working with data via AMF, and you have a Project value object with the properties ‘id’, ‘name’ and ’status’.

public class Project {
  public var id:Number;
  public var name:String;
  public var status:String;
}

In no time at all the rest of your code will be littered with references to these properties, and obviously if any of these properties changes during refactoring will be instantly picked up by the compiler. No biggie refactoring those. But there are instances where changes can slip under the radar.

For example (examples are always good), take a look at this snippet:

private function on_result(event:ResultEvent):void {
  this.status = event.result.status;
}

See the subtlety? In this instance, event.result is an Object, which is dynamically typed. Therefore accessing erroneous properties will not be picked up by the compiler, and this ultimately leads to subtle, frustrating bugs and loss of hair.

To avoid this, ensure you type cast dynamic objects to their appropriate types before accessing properties thusly:

private function on_result(event:ResultEvent):void {
  this.status = (event.result as Project).status;
}

Now if the status property is renamed during refactoring, the compiler will instantly pick it up and the world will seem a little brighter.

Jun 8

A question regarding the embedding of SWF content came up on the Ruby on Rails mailing list. In this instance, the problem was related to the tags used to embed a SWF in Internet Explorer and Mozilla browsers. Internet Explorer requires an <object> tag, while Mozilla (and most other) browsers require an <embed> tag.

Further to that, the ridiculous EOLAS patent requires changes in the way that objects are embedded in web browsers. One of the effects of this is that Internet Explorer users must now click a plugin in order to activate it (which is a problem for SWF files, especially if they are used in navigation, since two clicks are required to navigate).

There is a workaround, however, in the form of the extremely lightweight SWFObject javascript library. It is very easy to use, and actually I prefer this method of embedding SWF files since:

  • There is no need to deal with <embed> or <object> tags at all. It’s all handled for you behind the scenes.
  • Cross browser embedding is implied; the exact same code embeds a SWF in any browser automagically.
  • Passing parameters to a SWF is trivial.
  • Supplying alternative content for browsers without the Flash Player is trivial.
  • Embedding with SWFObject bypasses issues caused by the ridiculous EOLAS patent.

Because of these issues, SWFObject has quickly become the de-facto standard for embedding SWF files.

I was in the process of replying to the mailing list, but I figured it would be more useful to write up a blog post for everyone. So here is a short tutorial on embedding SWF content in Ruby on Rails using the SWFObject library.

  • Download the zip file from the SWFObject homepage ( direct link here)
  • Extract the contents of the zip file anywhere on your machine, and copy swfobject.js to #{RAILS_ROOT}/public/javascripts
  • Include swfobject.js in the head of your layout file using:
<%= javascript_include_tag 'swfobject' %>
  • Copy your SWF file(s) to #{RAILS_ROOT}/public/swf
  • Include the following code in the body of the template file in which you wish to embed the SWF content:
You need to upgrade your Flash Player This is replaced by the Flash content. Place your alternate content here and users without the Flash plugin or with Javascript turned off will see this. Content here allows you to leave out noscript tags.

And that’s all there is to it. More information is available on the SWFObject homepage :-)

UPDATE: Meekish kindly posted a comment pointing out a plugin for Ruby on Rails that provides a flashobject_tag to use directly in your views. More info here. Thanks Meekish!