Feed items

Practical example of need for prototypal inheritance

Alex Sexton wrote a wonderful article about how to use inheritance pattern to manage large piece of code. His code also has a pratical need for prototypal inheritance for writing modular code.
creating standard jQuery plugin
Given below is code that does exactly what Alex’s code does.

prototypal inheritance in JavaScript

One of the key features of JavaScript language is its support for prototype method. This feature could be used bring inheritance in JavaScript.

return false considered harmful in live

Checkout following jQuery code written with jQuery.1.4.2. What do you think will happen when first link is clicked.

$('a:first').live('click', function(){
log('clicked once');
return false;
});
$('a:first').live('click', function(){
log('clicked twice');
return false;
});

I was expecting that I would see both the messages. However jQuery only invokes the very first message.

Build a google chrome extension using jQuery in 90 seconds

Lately I have been following jQuery commits on github . If I look at a commit which fixes a bug then I would like to go and see the bug description. In order to do that I need to type url which would be something like this http://dev.jquery.com/ticket/6084 .

I wrote a chrome extension which would add a link to the bug at the end of commit message.

Simplest jQuery slideshow code explanation

Jonathan Snook wrote a blog titled Simplest jQuery SlideShow . Checkout the demo page .

The full JavaScript code in its entirety is given below. If you understand this code then you don’t need to read rest of the article.

Unit Testing JavaScript using Qunit . See live result and test code.

If you are in a hurry then take a look at these two links and you are done.

How jQuery selects elements using Sizzle

Introduction

jQuery’s motto is to select something and do something with it. As jQuery users, we provide the selection criteria and then we get busy with doing something with the result. This is a good thing. jQuery provides extermely simple API for selecting elements. If you are selecting ids then just prefix the name with ’#’. If you are selecting a class then prefix it with ’.’.

Inject for array

Inject is awesome. I was going through comments of this blog and learned something new today.

Given below is a case where I need to add all the numbers. One way to solve would be something like this.

ar = [[:a, 1], [:b, 2], [:c, 3]]
c = ar.inject(0) do |sum, record|
  sum += record[1]
end
puts c #=> 6

Here is another way to solve the same problem.

Ruby EventMachine: a short introduction

Introduction

Before answering what is EventMachine, first I will try to explain the problem that EventMachine solves.

A network server like http server or chat server usually works in a threaded model. What it means is that on a particular port, a process is listening for connections. When a client makes a connection then this process spawns a new thread and that thread is handed over the task of responding to that client. If server is getting too much traffic then the number of threads created by the main process goes up very quickly.

Running rake task in background and not blocking the port

In this screencast Ryan Bates explains how to execute a rake task in background on run time. This will make controller return the response to the user fast. In the meantime email will be sent through the background job.

In short this is the solution he proposes.