Archive for November, 2006

Another Flex search engine — and you can put it on your personalized Google home page

Ted Patrick recently created the Flex Search Engine. I’ve created another one, based on Google’s capability of creating your own custom search engines.

The official Flex Search Engine is supposed to eventually rank results based on star ratings given to various results by the user community. But the problem I had is that for now, since it’s still in alpha and doesn’t yet provide star ratings, the ranking of results isn’t very good.

So I created another one, based on Google’s “Custom Search Engine” feature. You can see it in the right-hand column of this blog: Just below the regular WordPress search box (which only searches my blog) is another search box, which searches Google, but only on sites related to Flex.

And one great thing about Google customized search engines is that you can add them to your personalized Google home page. Just click the “Add to Google” link to the right.

It currently searches the Flex 2 parts of adobe.com and all the MXNA and fullasagoog Flex-related blogs. (Anything else I should add?) Unfortunately, it does not search the flexcoders and flexcomponents lists which are hosted on Yahoo groups. Actually, I did add them to the list of locations to search, but it is not finding results from those sites. Why? Well, because Ted explained to me that, probably due to turf wars, Yahoo apparently doesn’t allow Google to return search results from Yahoo groups. Ugh. [Update: In the comments, James Ward pointed me to a mirror of the flexcoders and flexcomponents lists, so I've added those mirrors to the search, and that did the trick. Thanks James!]

In any case, the ranking of results is excellent, thanks to my clever system that I call PageRank (umm, okay I guess I can’t take credit for that):

  • A search for “button” returns the livedocs page for mx.controls.Button as the first hit. (And also some very useful advertisements for buttons, pins, and badges.)
  • A search for “modules” returns a blog entry from Roger Gonzalez, the modules guru, as the first hit.
  • A search for “item renderers” returns a quickstart article about item renderers as the first hit, and the livedocs documentation page as the second hit.

E4X tip: writing expressions to test attributes that may not exist

E4X is an awesome way to work with XML. But one common pitfall is trying to write an expression that tests the value of a particular attribute, when some of the XML nodes may not even have that attribute.

For example, let's say you have this XML in a variable called albums:

var albums:XML =
<albums>
  <album name="The Dark Side of the Moon" artist="Pink Floyd" year="1973" />
  <album name="Rubber Soul" artist="The Beatles" year="1965" />
</albums>

It's easy to write an expression to get all albums that were released in the 1970s:

var seventiesAlbums:XMLList = albums.album.(@year>= 1970 && @year <= 1979);

But let's say some of the XML nodes you are testing don't have a "year" attribute:

var albums:XML =
<albums>
  <album name="The Dark Side of the Moon" artist="Pink Floyd" year="1973" />
  <album name="Rubber Soul" artist="The Beatles" year="1965" />
  <album name="Haunted" artist="Poe" />
</albums>

In that case, unfortunately the conditional-test part of the above E4X expression, (@year >= 1970 && @year <= 1979), will throw an exception:

ReferenceError: Error #1065: Variable @year is not defined.

So, what to do? I've seen various complicated solutions to this problem; but to me the cleanest one is to replace @year with attribute("year") -- do this for any attribute that you don't know for sure will be present:

var seventiesAlbums:XMLList = albums.album.(attribute("year")>= 1970 && attribute("year") <= 1979);

True, this is more verbose and slightly uglier; but it's not too bad.

The reason this works is that the function XML.attribute() is documented as returning an empty XMLList when the attribute you request does not exist. But when you just say @year, that's like saying "trace(blah)" where blah is some variable that does not exist -- the player tries to find something called @year, and it can't, so it complains.