Saturday, January 2, 2010

jQuery toggle

I recently found myself in a discussion about the best way to do a toggle link. Im not saying the problem is hard, but how do you solve it nicely? Since we all hate ifs ( 2 links on why: here and here )

In jQuery there is a nice way to handle this via the toggle method. Like this:

// hide a list of related items to
}, function(){

This means if you click the link the text will be 'show' and all the list items will be hidden. The second time you click it the text will switch to 'hide' and the list items will be shown. According to the specs the toggle function could take any number of functions, which would allow you to do something crazy like:

function(){ ... },
function(){ ... },
function(){ ... },
function(){ ... }

Or why not:

function f() { ... };
function g() { ... };
function h() { ... };


But if the toggle-function is given no arguments it will toggles the visibility of the matched element. The pattern where a function behaves differently depending on the number of arguments its given is fairly typical to jQuery. For example: click() that triggers the click-event and click(function(){}) that listens for the click-event.

Friday, January 1, 2010

remember_me, passwordless login in bash.

If you find yourself having to log in to remote servers a lot, ssh-keys could be nice to use. If you don't know about ssh-keys, here is a nice introduction.

Its quite easy to set up keys following a tutorial as this. But once you have it generated, it could be quite a hassle to do all the required steps on every machine you need to access. Especially if you find your self having to access many machines, often with more than one user.

Anyways, here's a nice bash-function to set up your keys for you.

function remember_me {
ssh $1 'mkdir -p .ssh && touch .ssh/authorized_keys \
&& chmod 0700 .ssh \
&& chmod 0600 .ssh/authorized_keys \
&& cat >> .ssh/authorized_keys' <~/.ssh/
Add this to your ~/.bashrc or ~/.bash_profile file. Reload your settings using

source ~/.bashrc
or equivalent. Then just type

You will be prompted for your credentials and then. BAM! Your keys are set up.
On the subject; an other related tip is to setup your servers as aliases adding something like

alias server="ssh"
to your ~/.bashrc file.
If you set up the ssh-keys correctly you just have to type

And there you are. Now you're on the remote server.