agaskar.com

ragaskar's blog

Prototype.js, IE, and method not found

Prototype has two ways of calling a prototype method on an object. The first is the one most people will use: the object is -- to borrow a phrasing from a co-worker -- 'prototype-ized', and then methods can be directly called on it using dot syntax. In the second, static* methods may be called on an object by accessing the method via the class definition and passing in the object you'd like to operate on. To give an example:

Prototype-ized Style:

my_object.addClassName('my_class');

Static Method Style:

Element.addClassName(my_object,'my_class');

Now, in theory**, prototype will automagically prototype-ize any object you may be working on. Ie, when you try to send addClassName to my_object above, it checks my_object to see if it is a Prototype object (rather than just a regular javascript object), an if not, adds the methods. Turns out this can occasionally fail in IE. Yes, of course, just IE. 6 and 7. Turns out this is a HUGE pain to debug, especially since JSUnit seems unable to provide a line number when this occurs (my guess? The line number is left out because the actual error is occurring IN the minified Prototype include). In our case, this happened on when we were trying to operate on a Select element in a form, but I suspect this problem is not necessary limited to particular elements.

Recommendation? Think about calling ALL prototype methods in the second fashion unless an object has explicitly been given the prototype methods via dollar syntax. Yes, it's a LOT of extra typing, but even then, it probably still wouldn't have been eight hours worth of typing, which is how long it took us to track down this bug (although hopefully next time, we'll think of this first).

* This designation is a presumption on my part and may be technically incorrect. You get the idea.
** More like wild-assed guess, but again, from observation of the code I worked on I presume prototype did this under the hood.

apt-get: Sub-process bzip2 returned an error code (2)

If you get this error, it can seemingly be fixed by commenting out all your sources (in /etc/apt/sources.list, and possibly in /etc/apt/sources.list.d) and doing an 'apt-get update', then uncommenting them and 'apt-get update'-ing again.

Which really isn't so much a "fix", as it is a workaround, but hey, it works! I'm ok with it being magic for now.

As a side-note, if you're running etch, you want the git from the etch-backports repository (which started this whole mess for me). The etch stable version is 1.4.4, which appears to have a slightly different command set -- ie, 'git init' does not work. That was enough for me to want to make sure I'm using the latest. Not sure if there's incompatibilities between old and new versions of git, but no sense in taking any chances.

Reverting to XP from a dual-boot GRUB.

My old laptop (a Dell Inspiron 2200) was configured to run XP and Debian Etch. It turns out that I was using Photoshop and Illustrator enough that I rarely booted into Linux

Tangentially: I've since found that VMware on a sensibly speedy machine works fantastically: I've got an Ubuntu install running alongside XP, and it's very convenient to be able to switch into an Linux environment without needing to reboot. Out of 3 GB of RAM, it has 1 GB dedicated to it and 10 GB diskspace. Performance of the VM seems fine (I've turned on paravirtualization, fwiw) and seems to have no noticeable effect on my day to day usage of XP (although the only memory hungry XP app I use is Firefox).

As I'm using the Gateway m6824 as my primary machine now, my older laptop travels with my girlfriend to her job; due to the awkward partitioning decisions I made (out of a 40GB machine, I allotted 10GB for each primary boot, and then used the remaining 20GB space for file storage and Windows paging) it was starting to become troublesome for her to run. After the jump, I'll go into detail about how I rolled back to a pretty vanilla XP install on one partition without a reinstall.

Auto-completion with apt-get

tab completion with apt-get install is possible by sourcing the bash_completion script -- which should already live in /etc/bash_completion if you're running debian. Simply type:

source /etc/bash_completion

Very handy -- I've sometimes had to look at package names two or three times before typing them in correctly. This also enables tab completion for a number of other things.

To have bash_completion enabled every time you login, add the following line to your /etc/profile file:

source /etc/bash.bashrc

Then open /etc/bash.bashrc, and uncomment or add the following lines:

# enable bash completion in interactive shells
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

EDIT: I've read some complaints that bash_completion overrides or breaks typical tab-completion behavior. I haven't noticed any instances yet, but caveat emptor.

Django On Google App Engine -- First Thoughts.

I'm in the midst of converting a semi-complete Django application to deploy on Google Apps Engine. It's not going as smoothly as I'd like, to say the least. After the jump, my first thoughts on the subject and a couple gotchas I ran into.

Handling GET vars in django templates using custom template filters.

Django has some fantastic features for pretty URLs, but I haven't been able to find an elegant way to handle multiple GET vars.

Now, it may just be that making requests with GET vars isn't very 'django'-like, but I find GET vars are a very appropriate vector for view filters. Sticking sort and search filters into a URL is problematic, because it will result in unnecessarily long URLs filled with placeholders. After the jump, I look at some examples and explain how I've decided to solve the problem (for now).

Installing XP on a Gateway M-6824 Laptop

I'll admit it -- I gave up on Vista. It seemed like every 20 minutes the extra loud fan on my Gateway M-6824 would start whirring, and I was super-annoyed by the two-step flow to the Task Manager. (I hated the obviously tacked-on User Access Control, but that was quickly turned off). After the jump, a touch more whining about Vista and how to install XP on a M-6824.

BeautifulStoneSoup: Getting XML tag properties

BeautifulSoup is pretty easy to use, but I find the documentation confusing at times -- some of the simpler applications aren't covered in sufficient detail, or at all. One of these is a clear example regarding the fetching of XML tag attributes, a pretty common task. Getting a tag attribute is covered in the documentation here:


The attributes of Tags

Tag and NavigableString objects have lots of useful members, most of which are covered in Navigating the Parse Tree and Searching the Parse Tree. However, there's one aspect of Tag objects we'll cover here: the attributes.

SGML tags have attributes:. for instance, each of the <P> tags in the example HTML above has an "id" attribute and an "align" attribute. You can access a tag's attributes by treating the Tag object as though it were a dictionary...
[goes on to show an HTML example with ID attributes]

Although it's not made explicit, the same sort of syntax can be used to grab attributes from XML tags. This means that getting an XML attribute from BeautifulStoneSoup is as easy as soup.tag['attributename']. The one gotcha is that BeautifulStoneSoup converts everything to lowercase -- if the actual XML looks like <tag AttributeName="foo">, then soup.tag['AttributeName'] raises an exception. This part isn't so well-documented.

Let's look at a brief real world example after the jump.

Super simple Drupal clean URLs with lighttpd.

Agaskar.com runs lighttpd to squeeze the most it can out of a tiny 64M VPS (which, believe it or not, Drupal 6.x can run on -- I usually have about 2-4M free at any given time, and probably could've had a bit more had I chosen a 32-bit architecture instead of a 64-bit one!) I've been meaning to turn on clean urls on agaskar.com for awhile, but everything I'd read about drupal clean urls with lighttpd seemed to be a bit of a hack (redirecting a 404 to the index.php page) or reputedly didn't work perfectly.

This URL rewrite solution using a LUA script, however, seems to work great and only took 5 minutes to drop in. Read on to learn about the minor changes to these instructions I made in order to get it working on my server.

Another reason to love django: the regroup template tag.

If you've ever done any CMS work, you'll know that you'll often want to group a list of database records together by date or another properties. Django makes this easy with the regroup template tag. Read on to find out how it works.

Syndicate content