Commands, Events, or Calling a Function Directly

I was talking with a coworker about the best way to trigger actions from a component in Flex. There are a few ways you can do it and each language has it’s preferred technique.

In Flex, you’d most likely handle the event dispatched by a component. Using a button click as an example:

<s:Button click="handleClick(event)"/>

In C# you’d use Commanding to hand a command class that encapsulates your function to be called to the button. Here’s a snippet from a Silverlight project:

<Button Content="Save" cal:Click.Command="{Binding Path=SaveOrderCommand}" />

Other languages such as Javascript and Ruby allow you to pass functions around as variables or even use anonymous functions that get called when an action is triggered. You’ve probably handed an anonymous function to an Ajax call in Javascript. Here’s a jQuery sample:

$.ajax({ url: "mypage.html", context: document.body, success: function(){
// Do something on success
}});

Actionscript also allows you to pass functions around but events seem to be the “Flex way” of doing things.

Events, commands, or functions are all legitimate ways to call a function when an action needs to be handled. I find I stick with the technique that’s best supported by the environment I’m working in.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Powered by WP Hashcash