Skip to content


New design!

I finally got around to updating the theme for this blog. The last theme was nice but it had too many bugs and didn’t fit well with the content. I think this design is a nice fit and I like the code behind it.

There were some minor issues and probably more to come but all in all I’m happy with this new design. I plan on adding more features and content to this site so stay tuned.

Posted in Site News. Tagged with , , .

PureJSTemplate - A pure javascript templating engine for jQuery

Most templating languages suck

Ok, if your like me then you hate all templating langauges- they suck, they really suck. I hate them because I hate the rules they enforce upon me, they are slow, and they slow me down. Because of this, usually I end up making code that looks like this:

UGLY:

1
2
3
4
function display(data) {
   var output = "<div>" + data.text + "</div>";
   element.innerHTML=output;
}

How horrible is that? That’s probably worse than using a bad templating langauge.

Well, after disappointments with other jQuery templating plugins, I decided to make my own:

The Solution: PureJSTemplate

With PureJSTemplate you use old fashioned javascript in your template. You simply surround the javascript in special tags. Here’s an example of what you can do:

1
2
3
<# for(var i=0; i<10; i++) { #>
   <#=i#><br/>
<#}#>

That will output the numbers 0 through 9. Easy, isn’t it?

Using it:

You simply surround your template code with a textarea tag:

1
2
3
4
5
<textarea id="tpl" style="display:none">
 
<!-- TEMPLATE CODE HERE -->
 
</textarea>

and call it from javascript like so:

1
2
3
4
$("output").pureJSTemplate({
   id   : "tpl",
   data : {}
});

That’s it.

Get the Code:

Visit a demo/benchmark page here. Get the js here.

Posted in HTML/XHTML, JavaScript, jquery. Tagged with , , .

GWT + Grizzly + Comet

Ok, so you’re probably looking for a simple example of GWT + Grizzly + Comet. Jean Francois and his buddies at JavaOne screwed us with their vague PDF. Bastards! I gotta say this, though, Jean was right, its not too hard to figure out.

This implementation uses XHR long polling, glassfishv2.ur2 and grizzly comet 1.0.9.

The problem: RemoteServiceServlet

GWT loves POST. All RPC calls made back to your subclass of RemoteServiceServlet will hit the doPost method. However, this method is final and cannot be overridden - meaning we cannot create a comet handler for the request in our subclass of RemoteServiceServlet. We want to continue using RPC but we don’t want to use RemoteServiceServlet. WTF?

The solution: SimpleServiceServlet

Pretty much the RemoteServiceServlet is too secretive. So I pulled out its code and gutted it- creating SimpleServiceServlet. It’s new doPost method calls doConnect which you can override in your subclass - allowing you to create a comet handler. Yay.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class CometServiceImpl extends SimpleServiceServlet implements CometService {
   public CometContext cc;
 
   public CometServiceImpl() {
   }
 
   class GWTCometHandler implements CometHandler<HttpServletResponse> {
      ....
   }
 
   public void doConnect(HttpServletRequest req, HttpServletResponse res) {
      try {
         res.setContentType("text/html;charset=ISO-8859-1");
         GWTCometHandler ch = new GWTCometHandler();
         ch.attach(res);
         ch.attachRequest(req);
         cc.addCometHandler(ch);
      } catch (Exception e) {
         System.out.println("connect: e=" + e);
      }
   }
}

Other issues: The RPC Thing

GWT expects a return value from RPC methods. But what about in the case of comet? There technically isn’t an RPC method - a connection is established and then the client waits. Well, lets not re-write GWT, lets just make it happy. In traditional GWT RPC apps we extend the “RemoteService” interface that’s used by the server and the client. Let’s go ahead and do that:

1
2
3
public interface CometService extends RemoteService {
   Message comet();
}

And lets also create the object we are going to return via comet and RPC:

1
2
3
4
5
6
7
8
9
10
11
public class Message implements IsSerializable{
   private String text;
 
   public void setText(String text) {
      this.text=text;
   }
 
   public String getText() {
      return text;
   }
}

So we basically follow the pattern perscribed to us by GWT - but heres the catch. In our implementation of CometService we do the following:

1
2
3
public Message comet() {
   return null;
}

Yes, we return null! This way we make GWT happy - it can continue to do all of its fancy RPC’ing. And we can do comet without any hassles. But, how do we return serialized objects to the client?

Return serialized objects?

We do it in our subclass of grizzly’s CometHandler -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class GWTCometHandler implements CometHandler<HttpServletResponse> {
   HttpServletResponse res;
   HttpServletRequest req;
   public void onEvent(CometEvent ce) throws IOException {
      System.out.println("onEvent");
      // GWTEvent event = (GWTEvent) ce.attachment();
      Message msg = (Message) ce.attachment();
      try {
         //LETS SERIALIZE!!!
         String encoded = RPC.encodeResponseForSuccess(CometServiceImpl.class.getMethod("comet"), msg);
         //LETS WRITE THE RESPONSE
         writeResponse(req, res, encoded);
         removeThisFromContext();
      } catch (SecurityException e) {
         e.printStackTrace();
      } catch (SerializationException e) {
         e.printStackTrace();
      } catch (NoSuchMethodException e) {
         e.printStackTrace();
      }
   }
   ......
}

The Code:

A good sample app is worth a thousand words - download it here. This is a very simple and rough attempt at getting GWT working with Grizzly Comet. I’ll be posting an improved version at some point. It does not work in the GWT shell. You will have to deploy it to glassfish.

Posted in Java, JavaScript. Tagged with , , , .

Adobe Air 1.0 Released today

Adobe has officially released Air 1.0 today. I must have been under a rock somewhere. I didn’t expect this product to be out of beta anytime soon. Guess I was to busy going crazy about other products (*cough*… Jaxer).

Its a great time to be a Javascript developer (aka a JavaScriptr). So many things going on.

Find out more about Adobe Air here. Happy Scripting!

Posted in Adobe Air, News. Tagged with , , , .

Aptana Jaxer can change the game!

What is Aptana? What is Jaxer? What does this have to do with JavaScript? Let me fill you in if you never heard these two words in the JavaScript community before.

First, what is Aptana?
Aptana Inc. is a company founded by Paul Colton back in 2005 (Paul created JRun back in the day). The company’s first product is an open source JavaScript IDE called, you guessed it, Aptana which is based on the Eclipse platform. The IDE name was recently changed to Aptana Studio. It’s a pretty popular product that many JavaScript developers use. There are about 1,321,900 downloads to date according to counter on the Aptana website.

Ok, now what is Jaxer?
Jaxer is the latest product from Aptana. It is “The world’s first Ajax server”. Basically its an application server that speaks JavaScript. This means you can code your entire web application (front and back end) using JavaScript, HTML & CSS only! Aptana Jaxer is also an open source project licensed under the GPL. The JavaScript and DOM engine is the same one used in Firefox 3 (Mozilla). This means you can write your code using the latest JavaScript specs (1.5 to 1.8).

Cool! Now how will this change the game?
Are you kidding me? In my career as a web developer (since 1998) I’ve written code in VB, Tango, Perl, ColdFusion, Java, PHP and a bunch of unheard-of proprietary languages. The only programming language I’ve used in all my projects is JavaScript. Imagine a world where you don’t need to worry about all those other server side languages. Who needs Ruby on Rails? Why not use the most popular programming language on the web?

Now I know its a bit early to dump server languages like Java, PHP and ROR. Jaxer is still in beta (v0.9) and they just released a version for Linux. So we still have a long way to go but since this is all open source, I really hope the see it take off. I plan on doing my part so look out for future articles about applications I’ll be porting over to Jaxer.

Read more about Jaxer at http://www.aptana.com/jaxer

Posted in Ajax, Aptana, JavaScript, Jaxer. Tagged with , , , , .

JQuery on Rails

The JRails library is a useful tool for programmers working on Rails.

jRails is a drop-in jQuery replacement for Prototype/script.aculo.us on Rails. Using jRails, you can get all of the same default Rails helpers for JavaScript functionality using the lighter jQuery library.

I plan to start programming with Rails soon so I’ll definitely be adding this to my arsenal.

Posted in JavaScript, Quick Link, Resources. Tagged with , , , , .

Learning Adobe Air

Jonathan Snook has a great article on 24ways.org about creating an Adobe Air application using JavaScript, HTML and CSS. This is a great way for web developers who don’t know much or don’t care about Flash to get started.

I’d like to push this further by creating a desktop app that can also live on the web. Maybe even communicate with the desktop version.

You can check out Snook’s article here.

Posted in Adobe Air, HTML/XHTML, JavaScript, Quick Link, Resources. Tagged with , , , , , .