Viewing by month: May 2008

May 30 2008

Scotch 2008: The glass is half full!

It's almost time to fly out for Scotch on the Rocks where I'll be talking about Model-Glue 3 and combining ColdFusion with Hibernate. In celebration, I snapped a few shots of a ColdFusion tumbler that I got as "thank-you" swag from an Adobe event being filled with a beverage (Not actually Scotch. This amount in one glass would probably kill me.). If you like it, links below are to desktop-sized images for 17' and 15' MacBook Pros and the full-size image (warning: it's 6MB).

1440 wide (suits 15' MBP)

1680 (suits 17' MBP)

Full size

2 comments - Posted by Joe Rinehart at 8:03 AM - Categories: Conferences and Speaking Engagements

May 22 2008

Powerful MXML Bindings with Ternary (?:) Operators

One thing I've found myself doing more and more often in Flex applications is using Ternary operators within data bindings. Frequently, if I find myself with an event handler that updates visual state based on nothing more than a condition, I can replace the AS3 function entirely with such a binding.

A example over on the Flexexamples blog that was posted earlier today shows a good "before" picture of the situation. A change handler is assigned to a checkbox, and based on the state of the checkbox, an error string is set / nulled:

<mx:Script>
<![CDATA[
private function checkBox_change(evt:Event):void {
   if (checkBox.selected) {
      checkBox.errorString = "";
   } else {
      checkBox.errorString = "You must click here to continue";
   }
}
]]>
</mx:Script>

<mx:CheckBox
   id="checkBox"
   label="I have read and agreed to your license"
   errorString="You must click here to continue"
   change="checkBox_change(event);"
   width="100%"
/>
Full code available at Flexexamples.

What's the Ternary operator?

It's a shortcut to a conditional operator that uses a ?: combination as a quick "if / then / else" to return one of two values. It can replace the following code:

if (foo == bar)
{
   result = "success";
}
else
{
   result = "failure";
}

With this:

result = (foo == bar ? "success" : "failure");

What's important about it in this context is that it's legal to use it within a data binding expression.

Putting it all together

By using a ternary operator in a binding expression to control the CheckBox's error string, we can replace the AS3 event handler in the Flexexample code entirely, leaving us with nothing but MXML:

<mx:CheckBox
   id="checkBox"
   label="I have read and agreed to your license"
   errorString="{checkBox.selected ? null : checkBoxErrorString}"
   width="100%"
/>

Other uses

I find myself often using this to control whether or not a control is enabled/disabled while a server-side operation takes place:

<mx:Panel
   title="Edit User: John Doe"
   enabled="{this.saveStatus != AsynchonousStatus.WAITNG}"
/>

I'll also use it to show / hide elements when a full-bore state may not be necessary:

<mx:HBox>
   <mx:Button label="Home" />
   <mx:Button
      label="User Management"
      includeInLayout="{currentUser.isAdministrator}"
      visible="{currentUser.isAdministrator}"
   />

</mx:HBox>

Conclusion

Data binding allows us to work declaratively, and by using it to evaluate expressions as well as update data, we can avoid a good deal of manual event handling.

4 comments - Posted by Joe Rinehart at 9:00 AM - Categories: Flex

May 21 2008

Model-Glue 3 Roadshow: Online, Live, 7:30pm Eastern, Tonight

I'll be presenting the Model-Glue 3 Roadshow tonight at 7:30pm for the Hartford, Connecticut CFUG (http://www.ctcfug.com). Sorry for the late notice, but we weren't sure it was going to happen until about five minutes ago!

For anyone who'd like to attend, the Connect URL is http://adobechats.adobe.acrobat.com/ctcfug200805/. If you can't make it but would like to see the presentation, I'll be doing more online sessions this Spring and Summer. It's also the same presentation that I'll be giving at Scotch on the Rocks and CFUnited and have given at cf.Objective() and WebManiacs.

5 comments - Posted by Joe Rinehart at 3:24 PM - Categories: Model-Glue

May 6 2008

Groovy + ColdFusion = Happy

Over the past month or so, I've become less and less enchanted with using ColdFusion Components to model my applications. They're verbose, and they don't support many constructs I've gotten used to in Actionscript 3.

I've started looking at Groovy, a dynamic language that compiles to the JVM, as an alternative. In theory, results in true Java classes that ColdFusion can use like any other Java class, but is also a truly dynamic language.

I was pleased to find that it simply works: creating a Java project in Eclipse, adding a Groovy class to it, and exporting a .jar into WEB-INF/lib makes the compiled Java version of the Groovy class available upon restarting ColdFusion, just like writing Java. Woot!

Here's the thing, though: you still have this code -> compile -> restart ColdFusion bit to deal with.

I've almost fixed that - I've got a GroovyLoader CFC here that wraps GroovyClassLoader, allowing dynamic compilation of Groovy classes into a running instance of ColdFusion. I'm going to clean it up and test it some on my flight (boarding now!), and hopefully post it later this week...

10 comments - Posted by Joe Rinehart at 10:46 AM - Categories: Joe Drinks Java