Fluid Releases JS Api

I [previously](http://www.simpltry.com/2008/01/01/fluid/ “Simpltry » Blog Archive » Fluid”) blogged about [fluid](http://fluidapp.com/ “Fluid - Free Site Specific Browser for Mac OS X Leopard”). I said, “I would be surprised if they don’t expose a plugin framework.” I am happy to report I was wrong. They exposed a JS API, to get results like:

fluid js api

The reason a js api is better than a plugin framework, is because a user doesn’t need to build a plugin, they can simply include 3 lines of code in their app and they get custom doc badges. The latest version (.7) of fluid also added [growl notification](http://growl.info/ “Welcome to Growl!”) with fluid, which is cool. Anyway here is the 3 lines of code.

if(window.fluid) {
    window.fluid.setDockBadge("7");
}

Even if Fluid doesn’t stick, which I hope it does, this is certainly the a key to what the future of software development looks like.

P3P - Not My Idea of a Good Time

Maybe I am stupid. After spending 6 - 10 hours wondering why my site doesn’t work in an iFrame in [IE](http://www.microsoft.com/windows/products/winfamily/ie/default.mspx “Internet Explorer: Home Page”), I was beginning to suspect the worst. Of course, I wouldn’t be posting to this blog without the solution.

Basically, if you are in a frame/iFrame with a different domain than the window, you are a 3rd party. Unless you set a [P3P](http://www.w3.org/P3P/ “P3P: The Platform for Privacy Preferences”) header, IE will block all cookies with extreme prejudice. In rails this can be accomplished by using a **before\_filter**, to set a **response.headers['P3P']** value to something reasonable. If you don’t get through the spec, [this page](http://tumblelog.marco.org/post/1287369 “Marco.org Tumblelog - P3P sucks: How to get frame cookies unblocked in IE6 and IE7″) seems reasonable.

The real annoyance comes from the fact that P3P is so forgettable. Every seasoned developer I mention the solution to, said they exact same thing, “Oh yeah, our company ran into that, what a crock . . . “

ruby-debug meet TextMate

I love [Ruby](http://www.ruby-lang.org/ “Ruby Programming Language”) and [TextMate](http://macromates.com/ “TextMate — The Missing Editor for Mac OS X”). So when my coworker said, “come on, ruby-debug opens in TextMate, it’s a conspiracy”, I nearly jumped for joy.

As of rails 2+, you can interactively debug **”script/server -u”**. One of the commands is **tmate**:

ruby-debug

After you type that, it will open up TextMate with the cursor on the correct line:

ruby-debug-textmate

Once inside TextMate, you can’t really do anything interactively. But, it does become kinda handy when stepping into rails code, you can take a look at the whole method to get a little context.

Pretty Cool!

CSS3 Selectors and the $$() Method

I think an oft overlooked feature of the $$ utility method and other similar methods - select(), up(), down(), etc. - is their (almost) full css3 selector support. These advanced selectors have been available since prototype 1.5.1 (and I believe similar functionality is found in jQuery), but I often see code that doesn’t take advantage of them. Take the following markup:

<div class="example">
<p class="inner" title="Block 1">Block 1</p>
</div>
<div class="example">

Block 2</p> </div> <div class="example"> <p class="inner" title="Block 3">Block 3</p> </div> <div class="example" id="example-4"> <p class="inner" title="Block 4">Block 4</p> </div>

When selecting portions of the preceeding code, one might be tempted to use the $$ method combined with a loop.

// shade the background of even numbered divs
$$('.example').each(function(div,i){
  if(i%2==0) div.setStyle({background: '#ccc'});
});

A more concise, and efficient, way to achieve this effect would be to use css3 selectors.

  $$('.example:nth-child(even)').invoke(
    'setStyle',{background: '#ccc'}
  );

The :nth-child pseudo-class is part of the css3 specification. The class takes one argument. You could use ‘even’ or ‘odd’ as we did above or get a bit more complicated by using the format an[ + b]. Sounds a bit complicated, but once you see a few examples it becomes clearer.

// another way to select all the even divs
$$('.example:nth-child(2n)');

// select every third div
$$('.example:nth-child(3n)');

// get all the even divs starting with
//div number three (only the fourth div)
$$('.example:nth-child(2n+3)');

// simpler syntax to select only the fourth div
$$('.example:nth-child(4)');

Other useful filters include :last-of-type, :first-of-type and :not(). Their functions should be self-explanatory, but lets look at the syntax.

// pick the last div
$$('.example:last-of-type');

// pick the first div
$$('.example:first-of-type');

// pick anything that doesn't have an
// id of 'example-4'
$$('.example:not(#example-4)');

One final selector I want to discuss is the attribute selector. While its simplest incarnation may be familiar to many developers its more powerful than many realize. Allowing selections based on a tag having a specific attribute, having an attribute with a specific value, or even based on some simple regex-like substring searching.

// simplest incarnation any paragraph
// that has a title attribute
$$('p[title]');

// title attribute with a specific value
$$('p[title="Block 1"]');

// title attribute with a value
// that BEGINS with 'Block'
// selects every p tag in our example
$$('p[title^="Block"]');

// title attribute with a value that
// contains the letters 'ock' somewhere
// in the string. Again would select
// all of our p tags
$$('p[title*="ock"]');

// this selector is very useful in forms
// especially with the input tag to only
// select certain types of inputs
$$('input[type="hidden"]');

While powerful on their own, these selectors become amazingly flexible when combined. There really should be very few instances where you find yourself in a situation where you can’t find the correct DOM node.

// select all the even divs that don't
// have an id of example-4
$$('.example:nth-child(even):not(#example-4)');

// select all the even divs that are not
// the last sibling div and don't have a
// title attribute
$$('div:nth-child(even):not(:last-of-type):not(div[title])');

There are several more selectors you should familiarize yourself with, all of which are explained in the specification. If you find yourself having problems getting the syntax exactly right, I highly recommend the Selectoracle. A utility that will explain css selectors in plain english.

One Last Thing

don’t forget about the select() method. By using this method you can limit the amount of the DOM your script has to search over to match your selectors. Instead of searching the entire your document, you can specify the id of an element to use as the bounds of the selector

// find the even number divs that are inside
// the #myId element
$('myId').select('div:nth-child(even'));

That’s all folks, happy selecting. If you have any particularly useful selectors, show em off in the comments.

About Burning Bridges

I have always been a huge fan of [Zed Shaw's](http://www.zedshaw.com/ “ZSFA — All About Me”) tell it like it is style. He posted a [rant titled "Rails Is A Ghetto"](http://www.zedshaw.com/rants/rails_is_a_ghetto.html “ZSFA — Rails Is A Ghetto (2007-12-31)”) yesterday which you may or may not have read. He obviously doesn’t want to work with rails, which is really a shame for all of us who intend to stay in the community. It is because of his work ([Mongrel](http://mongrel.rubyforge.org/ “Mongrel: Home”)) that I even consider releasing a large scale project on rails. What saddens me is that he feels betrayed by a community, that I consider myself a part of.

I think that a lot of what he writes could be applied to any community, especially the part about consultants. But there are plenty of rails specific things. And there are some things said about the rails community that I agree with. Here on the East Coast I have worked with mostly professional people, I wouldn’t call them geniuses or anything (myself included), but professional.

I don’t want to get into any specifics, because I am **not** about burning bridges.

Feeds/Syndication