ruzz' stream of semi-consciousness http://stream.ruzz.org when you give up caring, you give up.. caring. posterous.com Sat, 16 Oct 2010 09:06:00 -0700 jQuery 1.4.3 is out, here's why i care http://stream.ruzz.org/jquery-143-is-out-heres-why-i-care-4 http://stream.ruzz.org/jquery-143-is-out-heres-why-i-care-4

another "minor" release in the 1.4.x line -- read the blog for more details: http://blog.jquery.com/2010/10/16/jquery-143-released/

it's got some cool stuff in it, and shows to me that the jQuery team has their shit together in a huge way. They call a release like this minor, where a lot of development teams would consider that much work a substantial release. In any case, I don't normally bother to write about release updates, but there's something pretty juicy in this one relating indirectly to templating via a small change in the .data() methods. 

first imagine a template like:

<div id="user" data-id="1" data-name="John">
Hi  {{name}}
</div>

previously you had to monkey about doing things like var id = $('#user").attr('data-id'); to get that id and pass it to your templating engine. if you were passing several bits of data this could be tedious so you probably improved the above template to look like this:

<div id="user" data-user="{ id: '1', name:'john'}">
Hi  {{name}}
</div>

allowing you to pull all your attributes in one object, but you still had to manually pull your attributes all the time. 

enter 1.4.3 and all html 5 data attributes (you have been using the data-xxx format right?) are automagically read into the elements .data() object. this seems minor but is going to save a lot of tedious work because if the above template is used now you can just call $('#user').data('user'); and have access to all those values right out of the gate. 

sometimes small intuitive changes have the biggest impact. 

I can immediately go through some code on the app i'm working on and trim out a bunch of .attr() calls saving me code. but more interestingly anywhere i've already been using the html5 data tags (and i have) i already have access to those as data if i need them and this is key for me. It encourages the proper use of a new standard (html5) and rewards you for adopting it early with less work. We need more improvements in our tools along this line because it's the quickest way to get new standards to be "the" standard. 

when you couple changes like the above with speed improvements and implementation improvements (like becoming more compatible with CommonJS) and changes borne from your user bases expectations (Calling .data(Object) no longer completely replaces the data object instead it extends the existing object, leaving the unspecified values in place) you get a really great project to work with, even for minor releases. 

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/597936/light_session_039.jpg http://posterous.com/users/5AqlMZNsWVSp i.m. ruzz ruzz i.m. ruzz
Sun, 22 Aug 2010 12:41:00 -0700 programming concepts: jQuery.delegate http://stream.ruzz.org/programming-concepts-jquerydelegate http://stream.ruzz.org/programming-concepts-jquerydelegate

i've long thought the real battle of becoming a great programmer comes in ingesting the right concepts. most programmers are pretty clever and once they grok something in a way that makes sense to them theres a moment of clarity and inspiration that comes from seeing all the interesting ways you they could apply this new idea. this process is the heart of programmer growth. Ignoring the cruft, those programmers who treat programming as a job, it's clear the real impediment to growth is getting the right conceptual explanation (through books, tutorials, play, experiments, blogs, et al). When we have the right conceptual model, complex ideas become simple and practical. your conceptual model is the expression of your understanding of the things you're working with and determines what calibre of programmer you're going to become. 

Interestingly, most top programmers rely on code to explain themselves because articulating their latent conceptual framework is either impossible, or the outline of it is so deeply stacked on other concepts it's like explaining the evolution of western philosophy. This conceptual stack problem is hard to get around. You can't accurately explain many sophisticated concepts without leaning on an underlying stack of other sophisticated concepts. Imagine giving Javascript: the good parts to a very junior programmer. They don't have a the basic concepts upon which much of the book relies and would get little from it. 

to that end, I've been thinking about a series of conceptual posts trying to boil an idea down to it's simplest conceptual explanation--which, im learning is often harder than the code itself to come up with. Lets give this a try with the oft misunderstood, but incredible helpful jQuery.delegate function. 

The existing concept:

from the api docs: http://api.jquery.com/delegate/

Delegate is an alternative to using the .live() method, allowing for each binding of event delegation to specific DOM elements. 

.delegate( selector, eventType, handler )

selectorA selector to filter the elements that trigger the event.

eventTypeA string containing one or more space-separated JavaScript event types, such as "click" or "keydown," or custom event names.

handlerA function to execute at the time the event is triggered.

$("table").delegate("td", "hover", function(){
        $(this).toggleClass("hover");
})

the above snippet will listen for hover events against "table" and fire the enclosed code when they happen. super clear right?

A new  concept:

jQuery.delegate is all about efficiency. I'm going to use the concept of a teacher and a classroom of students to lay this out in simple terms. Lets start by imagining a theres a teacher in a classroom that needs to know when each student is done their tests. Theres two ways to approach this:

  1. give each student individual instructions, one a time, to come tell the teacher when they're done.
  2. put instruction on the board for the everyone in the class telling them to come up and tell the teacher when they're done.

which is more efficient and flexible? in the first scenario what happens if another student comes into the room (perhaps by ajax :P) to take the test after the instructions have been given individually? and what happens if we change our minds about what the student needs to do when the test is done? do we have to go back and individually explain the new instructions?

lets try this as a bit of code:

 

$("classroom").delegate("student", "doneTheTest", function(){
        $(this).tellTheTeacher();  //$(this) == the student thats done right now
})

what we're saying is any student in the classroom thats done test should tell the teacher. If a new student comes in, they're covered by the same rules because they are in the "classroom". If we want to change our instructions, we change it once for all students, even the new ones because the instuctions aren't followed till they are done the test. 

thats all you need to know to apply this concept and get around a lot of unsightly code handling individual items. If you're curious why it works this way, you should follow up with some reading on event bubbling in javascript. but theres no reason you need to understand that to leverage the power of delegate. 

the one line concept:
anytime you have a group of things you want to watch for something, delegate it to the container they all belong to. 

I hope to roll out a few more of these as time permits. Right now i'm heavy into jQuery so I think the next one will be the oft misunderstood, but wow powered jQuery.data() which has the power to transform the very way you think about the dom and your data. 

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/597936/light_session_039.jpg http://posterous.com/users/5AqlMZNsWVSp i.m. ruzz ruzz i.m. ruzz
Thu, 19 Aug 2010 12:04:02 -0700 John Resig's jQuery Ninja talk at Tec... | Zoopy | Share videos, photos and audio clips http://stream.ruzz.org/john-resigs-jquery-ninja-talk-at-tec-zoopy-sh http://stream.ruzz.org/john-resigs-jquery-ninja-talk-at-tec-zoopy-sh

the deeper you go down the javascript application hole, the closer i think you get to the idea that the dom and the data are the same. MVC patterns start to fall apart. if you watch this watch at least to the part where he explains the logic of the small game he wrote. it's a gamble he's taking by being a heretic, but one i think he'll win in the end.

as an aside, isn't in troubling or, interesting, that this was shot at the tech4africa confrence and almost everyone in the audience is white, middle aged men.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/597936/light_session_039.jpg http://posterous.com/users/5AqlMZNsWVSp i.m. ruzz ruzz i.m. ruzz