Two reasons to love underscore.js

I’ve recently started to include undescore.js library to all JavaScript applications and sites I create. Underscore is a small library providing “a lot of the functional programming support that you would expect in Prototype.js (or Ruby)”. I originally found this library when using Backbone.js framework.

Underscore provides about 60 functions, but there are two that I’ve found especially useful.

1. bindAll
I’ve never really understood JavaScript’s this-context and why it’s implemented so oddly. When binding object’s method as an event handler, object context is lost making this-keyword fairly useless.

But hopefully we have Underscore and bindAll. It binds a number of methods on the object to be run in the context of that object whenever they are invoked.

See this example stoled directly from documentation:

var buttonView = {
label : 'underscore',
onClick : function(){ alert('clicked: ' + this.label); },
onHover : function(){ console.log('hovering: ' + this.label); }
};
_.bindAll(buttonView);
jQuery('#underscore_button').bind('click', buttonView.onClick);
=> When the button is clicked, this.label will have the correct value...

2. Templates
I found myself writing a code like this:
var html = "<h1>Menu</h1><ul><li>"+item1+"</li><li>"+item2+"</li></ul>"

Which is… wrong. You should never mix functional code and presentation. Underscore has a simple template function, which allows you to separate markup from JavaScript.

You can define template on your html-source:

<script id="my-template" type="javascript/template">
<h1>Menu</h1>
<li><%= item1 %></li>
<li><%= item2 %></li>
</script>

And then render it on JavaScript side:

var template = _.template(jQuery("#my-template").html());
var html = template({ item1: item1, item2: item2 });

No more messy html within JavaScript!

With these two simple functions my life gets much easier :)

Caching matters

We are developing a WordPress site for a popular brand and we’re expecting high traffic. So I installed WordPress Super Cache plugin and did some measurements with Apache Benchmark. Results are obvious (requests per second, 10 concurrent users):

WordPress caching performance compared to non-cached site

This if nothing proves that caching matters :)

Tutorial: How to use Photoshop’s smart objects?

I’ve come across many designers who don’t seem to know about “smart objects”. Smart objects are a way to save bitmap data in a external photoshop file. This enables us to group multiple objects/layers into a single layer without flattening it. Trust me, knowing smart objects will save you a lot of time and effort.

Continue reading

Why YOU should have a side project

I just read a great blog post by Matt Ward about web developers/designers and side projects. Matt says that everyone should have have a side project and he told four reasons why:

  • To show your passion.
  • Experimenting new techniques.
  • Recognition by the community.
  • Maybe a chance to earn some extra revenue.

I’m running couple of side projects and I completely agree what Matt says. I’m for example publishing an weekly online comic Office Animals, which tells about nerd animals. At first it might sound something that’s not very related to my daily work. But actually it is. The comic has taught me lot of things, for example in these areas:

  • Web analytics.
  • Customer engagement and Facebook marketing.
  • Blogging.
  • Technical WordPress details.

And the last but not the least: drawing a comic is very energizing and refreshing for me. I’ve had times when I’ve felt that all my schedules are ruined and nothing is going as expected. But the comic is something that succeeds week after week and gives me strength to push forward.

So side project is something that I can honestly recommend for everyone. If you don’t have your own, you really should consider starting one :)

Makeshift php-hamlet

Html sucks. If you don’t believe, try using HAML for a while.

Haml rules. Integrating haml with rails is trivial. Integrating haml with php/wordpress is not. Yeah there are multiple plugins that either work poorly or don’t work at all. Besides in our projects, it would be nicer to have .php.haml files pre-processed.

For that case, I kicked up a simple wrapper for Fammel php-haml-doodamadingy. More about fammel/phaml in https://github.com/dxw/Fammel/wiki/Documentation

This wrapper called “Hamlet”, at least till we get sued or invent a better name.
You can clone it with git from http://evermade.fi/hamler/

$git clone http://evermade.fi/hamlet/ MY_HAML_FOLDER

you can find 3 folders inside. haml is where you place your haml-files as FILENAME.php.haml and compiled is where they appear as FILENAME.php. Fammel-folder contains the fammel-stuff that actually conjures php from haml. watcher.rb is a ruby script that tracks files in the haml-folder and if a new one appears or an old one is updated, that file is hamlified to compiled-folder, resulting in well-formed and valid PHP.

Start watcher with

$ruby watcher.rb

and kill it with ctrl+c

You can bolt this widget to pretty much any given project by simply replacing the compiled and haml-folders with simple symlinks.