Using the Value Attribute

Whether you realize it or not, you often use a subclass of the basevaluecomponent in OpenLaszlo. A typical example is radiobutton (which is used with radiogroup). The example for radiogroup in the OpenLaszlo reference is pretty simple:

<canvas debug="true">
    <radiogroup id="group1">
        <radiobutton value="1" text="one"/>
        <radiobutton value="2" text="two" selected="true"/>
        <radiobutton value="3" text="three"/>
    </radiogroup>
</canvas>

I modified it a little, to include the debugger, but note that the values for the radiobuttons are “1”, “2” and “3”. If you decide that you need to use values such as “one”, “two” and “three” instead of numbers, the first thing you might try is something like this:

<!-- WARNING: BAD EXAMPLE -->
<canvas debug="true">
    <radiogroup id="group1">
        <radiobutton value="one" text="one"/>
        <radiobutton value="two" text="two" selected="true"/>
        <radiobutton value="three" text="three"/>
    </radiogroup>
</canvas>

However, that will lead to debugger warnings along the lines of:

ERROR @filename.lzx#5: reference to undefined variable ‘three’
ERROR @filename.lzx#4: reference to undefined variable ‘two’
ERROR @filename.lzx#3: reference to undefined variable ‘one’

The issue is that the value attribute is of type expression. That means it has to be a valid JavaScript expression. Think of what you can put on the right-hand side of an equals sign in JavaScript. If you put the number 1 (I’ve omitted the quotes here intentionally), you’ll assign the number 1. If you put the characters one (again, quotes omitted intentionally), then the runtime will search for a variable with the name “one”.

What you really want is the string “one”, so you need to quote it. The double quotes are the XML attribute quotes. Since in JavaScript, you can alternate between single- and double-quotes freely, the easiest solution is to use single-quotes within the double-quotes, to specify a string. e.g. value=”‘one'”. The above example would then look like:

<!-- CORRECT -->
<canvas debug="true">
    <radiogroup id="group1">
        <radiobutton value="'one'" text="one"/>
        <radiobutton value="'two'" text="two" selected="true"/>
        <radiobutton value="'three'" text="three"/>
    </radiogroup>
</canvas>

Empty Handlers in OpenLaszlo 4.1

The current release of OpenLaszlo doesn’t like empty handler tags. If you compile the following code, you’ll get a compiler warning:

<canvas debug="true">
    <handler name="oninit">

    </handler>
</canvas>

The warning will say:

“Refusing to compile an empty handler, you should declare the event instead”

This wasn’t the case previously. The warning is intended to educate developers who continue to define empty handlers to “hack” a custom event. (This is an old practice that is no longer necessary, since there is now an <event /> tag). If you’re intentionally writing empty handler tags (e.g. as stubs), those warnings may get in the way.

To work around them, provide a comment in your empty handler:

<canvas debug="true">
    <handler name="oninit">
        // Setup code for application.
    </handler>
</canvas>

Installing RMagick on Cygwin

RMagick is a Ruby to ImageMagick framework that’s needed if you want to edit images using Rails. FileColumn, a Rails plugin for handling file uploads, plugs into RMagick very smoothly. While FileColumn is really simple to use, installing RMagick on Cygwin seemed really daunting. There were a bunch of warnings on how it differs from a usual Rails plugin install. So I’m summarizing my experience here:

  1. First install ImageMagick for Cygwin. Do this by updating Cygwin itself, using the Cygwin setup program. It’s the same approach as you use for installing Cygwin the first time around. When you get to the Select Packages page, find the ImageMagick packages under the Graphics node and make sure they’re set to be installed. Look for libImageMagick1; there are a few others.
  2. Run gem update –system
  3. Download the RMagick gem for Linux. Not the Win32 one. That’s the one called “rmagick”. You have to download it; you can’t install it remotely.
  4. From the directory where you downloaded the RMagick gem, run gem install rmagick –local
  5. Rebase Cygwin. I’ve never had to do this before, but I got a bunch of errors in the Ruby server output console saying the following when RMagick was used:
    unable to remap C:\cygwin\usr\X11R6\bin\cygXext-6.dll to same address ruby died waiting for dll loading, errno 11
    To rebase Cygwin, close all your Cygwin shells, and open an ash shell by going to Start>Run…, then enter C:\cygwin\bin\ash.exe in the Run dialog and click OK. Run /bin/rebaseall from the ash console.

Then restart the Rails server from script.

For reference, I used RMagick 2.5.2, with Ruby 1.8.2 and Rails 2.1.0.