jQuery $.live() equivalent in YUI 3

Thursday, August 26th, 2010 | code, development, javascript, jquery, nerdery, tips, yui | No Comments

It’s been a while since I’ve posted and since I started at Yahoo! so I figured I’d give a little love to the new toys I’ve had the pleasure of playing with as of late. Clearly, we’re using YUI here at Yahoo! and because of that I’d had to adjust my thinking on a few goto front-end techniques that I relied on jQuery for. I hope to post more on these adjustments I’ve had to make, but we’ll see.

Aside from general patterns involving YUI loader, a major jQuery feature I realized that I had come to rely on is jQuery’s $.live() method.

Let’s say I have some markup like:


<div id="wrap">
    <span id="button">Click Me!</span>
</div>

That’s being dynamically inserted into the DOM.

I got pretty used to doing something like:


$("#id").live("click", function() {
    // do stuff
}

Super handy, but not exactly the best performance-wise. If you read the jQuery documentation (or even the jQuery source), you’ll soon find out that what’s going on behind the scenes is an implied event delegation to the root of the DOM. But, not to get too far off topic, there’s a lot of overhead that comes at the expense of this convenience.

YUI provides the same sort of functionality, albeit in a more explicit fashion via the Event delegate() method. I personally like the design decision that was made by the YUI team considering the performance hits that can be involved with using jQuery’s live method incorrectly, but to be completely fair, you can’t really mind the cleanliness of jQuery’s API. Anyways, you can always read the YUI 3 Event docs, but the above simply becomes:


YUI().use('node', 'node-event-delegate', function(Y) {
    Y.one('#wrap').delegate('click', function(e) {
        // do stuff
    }, '#id');
});

I really like the cleanliness of jQuery, but sometimes, you gotta make a little lemonade outta the lemons you were given, even if they’re a little bit rounder and less yellow than the ones you’ve seen before.

There are definitely some caveats in the jQuery way of doing things as well as in YUI, but I feel like it’s a whole lot harder to screw it up in YUI.

Now, go try it yourself.

Share The Wealth:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon

Tags: , , , , , ,

a new start

Monday, May 10th, 2010 | personal | No Comments

The last year has been a fun one. I moved to California and began working as an engineer at Mahalo.com. A huge opportunity! During this last year, I’ve had the luxury of working with a series of great engineers as well as some of the most interesting new technologies in the web space. We were lucky enough to solve really interesting problems in scalability and efficiency and were able to constantly push out new products and features, almost on a bi-weekly basis.

It was a very exciting time in my life, and now, an even more exciting time exists.

I was recently given an opportunity to work with another great bunch of developers and right here in Santa Monica, barely a mile away from where I live. It is an opportunity I couldn’t pass up. It has been made pretty clear from the various news outlets that my exit from Mahalo wasn’t on the best of terms, but I’d like to come out and say, that although it was a little rough, I wish all the best for Mahalo and it’s staff and I can’t thank them enough for such a great opportunity and learning experience. I will miss all the good times we had there, even if they were being had at 3am when product launch had gone awry. You guys are great.

But, I’m excited to be joining Yahoo! where I’ll be working mainly on Yahoo! Sports as a front end engineer. I’m pretty pumped to take on a role centered around UI engineering at a major internet company that just happens to own the largest sports site in the world. Here, I will hope to help inspire inovation, learn as much as possible from those around me, and build as many beautiful UIs as humanly possible.

I’m feeling pretty darn lucky right about now.

Cheers.

Share The Wealth:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon

Tags: , , ,

more node.js + comet tomfoolery

Wednesday, April 28th, 2010 | code, development, javascript, nerdery, node.js | No Comments

I know there have been quite a few people out there that have messed around with node and comet already, but I figured I’d give it a go perhaps if only to familiarize myself more with the awesomeness that is node.js. I was originally inspired by this post on Ajaxian, so you’ll notice that I too am basing this off of Faye’s implementation of the Bayeux protocol for publish/subscribe. Only real difference is that in my example, messages are passed to the client via stdin.

Here’s the code: http://github.com/eculver/node_comet

I also read in a few other places where people were using fs.watchFile or node’s process.createChildProcess to create a public activity log of sorts. Maybe I’m just a nerd, but it would be kind of cool to go to a high-trafficked site and see some of the server log data (origin, user-agent, etc) in real-time. Something to that effect.

Share The Wealth:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon

Tags: , ,

filtering special characters injected by microsoft word

Saturday, April 17th, 2010 | code, development, django, hacks, nerdery, python, tips | No Comments

I have been having trouble with users pasting content directly from M$ Word into admin fields, so I set out to create some sort of filter that replaces these little terds with something more HTML friendly. Here’s what I came up with, efficiency aside:

http://gist.github.com/370004

Please fork and enhance if you find more of these evil demon characters anywhere.

I guess it should also be noted that this can be called from any model save method on fields that you wish to filter.

Share The Wealth:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon

Tags: , , , , ,

curl-ca-bundle macports checksum woes

Tuesday, April 13th, 2010 | bash, code, hacks, nerdery | 1 Comment

Just got done pulling my hair out over some pretty stupid MacPorts problems, but eventually got things working again, so I figured I would share. My problem started with trying to upgrade curl to support SSL so that I could do git over http yadda yadda yadda. Little did I know that I could have just upgraded curl in the first place, but this serves to address another point. I’ll spare all the lame details, but in essence I got to a point where I was trying to re-install git-core but curl-ca-bundle, a dependency, was failing with this:

$ sudo port install curl-ca-bundle
---> Computing dependencies for curl-ca-bundle
---> Verifying checksum(s) for curl-ca-bundle
Error: Checksum (md5) mismatch for certdata-1.58.txt
Error: Checksum (sha1) mismatch for certdata-1.58.txt
Error: Checksum (rmd160) mismatch for certdata-1.58.txt
Error: Target org.macports.checksum returned: Unable to verify file checksums
Error: Status 1 encountered during processing.
Before reporting a bug, first run the command again with the -d flag to get complete output.

After some googling around, I found the bug and saw that it had been fixed. Great, the fix is only available in trunk. Ok so I’m thinking, I’ll just pull down trunk a build and run it. Why not I like to live dangerously:

Installing MacPorts from Subversion

Nope, that didn’t work either:

$ sudo ./configure --enable-readline
...
checking for existence of /usr/lib/tclConfig.sh... loading
checking for Tcl public headers... /usr/include
checking for tclsh... /opt/local/bin//tclsh
checking for Tcl package directory... /opt/local/lib/tcl8.5
checking whether tclsh was compiled with threads... no
configure: error: tcl wasn't compiled with threads enabled

Not really getting anywhere now am I. Dammit. But after scratching my head for a few minutes, I said, wait… this is all over a few lousy checksums? C’mon…So, in looking at the actual commit from the bug, I saw that it was just an updated Portfile and lightbulbs went off. All I had to do was copy the updated Portfile to my ports tree and port should see the updated checksums:

# cd to curl-ca-bundle
cd /opt/local/var/macports/sources/rsync.macports.org/release/ports/net/curl-ca-bundle

# backup Portfile just in case, even though it's totally jank
sudo cp Portfile{,.bak}

# replace Portfile with one from trunk
sudo cp /opt/mports/trunk/dports/net/curl-ca-bundle/Portfile .

# install package that depends on curl-ca-bundle, in my case, git-core
sudo port install git-core

Seems like if I can’t install git-core, a pretty common package, then a lot of people are probably having this same problem. Hope this helps, I was at a loss for quite a while.

Beer-thirty!

Share The Wealth:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon

Tags: , , , , ,

tab-complete SSH logins

Monday, April 5th, 2010 | bash, code, nerdery, tips | 2 Comments

A smart guy named Dave Hull (@davehull) shared this piece of bash magic with me a while back and I have relied on it ever since because it’s just that handy. It’s a bit of a stretch to say that typing out “ssh” every time is that annoying, but when you can tab-complete hostnames to login, pure bliss ensues. Ok, so to do this we just need one magical little bash script:

#!/bin/sh
ssh $(basename $0) $*

I usually name this “ssh-to”. Doesn’t really matter though. What matters is how you link it up. But first, make sure this is somewhere in your PATH, like ~/bin and also make sure it’s executable:

mv ssh-to ~/bin
chmod +x ~/bin/ssh-to

Last step is just symlinking to a hostname:

cd ~/bin
ln -s ssh-to some.host.com

And that’s it. You should be able to type “some.<tab>” and the hostname will be tab-completed. Super handy if you’re like me and login to the same hosts frequently.

Share The Wealth:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon

Tags: , , ,

Simple fix for python-memcached MemcachedKeyLengthError and MemcachedKeyCharacterError

Tuesday, March 30th, 2010 | code, development, django, nerdery, python | No Comments

Here at work, we recently ripped out cmemcache and replaced it with the more apache+mod_wsgi friendly python-memcached, only to be innundated with a bunch of these:

MemcachedKeyCharacterError: Control characters not allowed

as well as these:

MemcachedKeyLengthError: Key length is > 250

The good news was that we knew it was definitely related to our new memcache library that Django was falling back on, but the bad news was that we didn’t want to go through all of our code and filter/hash our keys for every call to cache.get() or cache.set(). My simple solution was to write a set of overrides to the default Django memcached backend. I went ahead and gist’d it if anyone is interested:

http://gist.github.com/349692

This solution truncates and filters the keys as opposed to hashing them like some would suggest. I believe either method should work fine for the most part, hashing offering a far lesser chance of key collisions, but I was a little scared to start hashing keys after reading how it bit Reddit in the ass:

http://www.royans.net/arch/reddit-learning-from-mistakes/

This seems to work well at alleviating our python-memcached woes since our keys should be safely unique after truncating. One thing to note though, that took me for a sec (because I was referring to Django trunk) is that `set_many` and `delete_many` are added in Django 1.2, so they will not work unless using 1.2 or unless your backend already implements them. All others methods should work though.

Cheers.

Share The Wealth:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon

Tags: , , , , , ,

notes on PIL + virtualenv + Ubuntu

Saturday, March 20th, 2010 | code, development, nerdery, python, tips | No Comments

Just some quick notes on installing PIL in a virtualenv in Ubuntu:

Most forums or message boards suggest:

sudo apt-get install python-imaging

to install PIL, but that doesn’t exactly do when you’re trying to install into a virtualenv with –no-site-packages, so… you try to install from source by downloading the tarball and doing the usual:

wget http://effbot.org/downloads/Imaging-1.1.7.tar.gz
tar -xzf Imaging-1.1.7.tar.gz
cd Imaging
sudo python setup.py install

In some cases this works, but other times build will fail with this:

...
_imaging.c:3138: warning: return type defaults to ‘int’
_imaging.c: In function ‘DL_EXPORT’:
_imaging.c:3138: error: expected declaration specifiers before ‘init_imaging’
_imaging.c:3149: error: expected ‘{’ at end of input

A little known fact is that if you first install python developer tools, the build will go smoothly:

sudo apt-get install python-dev

This may install a few packages other than just python2.6-dev, but whatever. You should now be able to build and install as usual:

sudo python setup.py install

Took me some poking around, so I figured I’d share, maybe save someone some time.

Share The Wealth:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon

Tags: , , ,

Handy One-Liners

Thursday, March 18th, 2010 | code, development, nerdery, python, tips | 3 Comments

It’s been a while since I’ve posted, so I figured I’d just go with something that I found to have been pretty useful as of late. Peteris Krumins recently posted a great article about one-liners, so I figured I’d add a few of my own to that list:

  1. Python SMTP Server

    python -m smtpd -n -c DebuggingServer localhost:1025

    This just starts an email consumer on your localhost on port 1025. When emails are sent to it, they are echoed to STDOUT instead of being routed. Handy when developing features that require email.

  2. Git Graph ‘Pretty Print’

    git log --graph --abbrev-commit --pretty=oneline --decorate

    This really just serves as a replacement to gitk or GitX in a (command-line) pinch.

  3. Remove .pyc

    find . -name '*.pyc' -exec rm {} \;

    Does just what you’d think it does: removes all .pyc files from the current directory you are in.

These are all pretty much just great candidates for bash aliases, but I figured I’d share since I was inspired. Thanks Peteris, and if you haven’t checked out his article, do so IMMEDIATELY:

Top Ten One-Liners from CommandLineFu Explained

Share The Wealth:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon

Tags: , ,

Getting Started with Rails – ERROR: could not find rails locally or in a repository

Friday, January 15th, 2010 | code, development, nerdery, rails, ruby | 2 Comments

Just wanted to share this with everyone directly since I had a little bit of a delay getting Rails installed and up to date. Per the “Getting Started” docs, I tried to install rails:

sudo gem install rails

But was stopped in my tracks with this bad boy:

ERROR: could not find rails locally or in a repository

After some googling around, I fixed it by first doing:

sudo gem update --system

And then:

sudo gem install rails

Just thought I’d share that, maybe save someone some time.

Share The Wealth:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon

Tags: , ,