Posts Tagged ‘Javascript’

Flurl – Part 5: The Unicorn/Panda Rainbow Connection

Wednesday, June 23rd, 2010

Wait, where’s parts 2 through 4? Not done yet, but I’m done with the project and I may never get around to posting those other parts and wanted to post the finished product.

Again, Flurl is a little practice exercise I did. A mashup of Flickr and Qurl and no external JS libraries used (so I wrote my own).

I’m taking this photo stream and sending the URLs to Qurl for shortening (using their API). This is the end result (best experienced in Chrome): The Unicorn/Panda Rainbow Connection (Be careful, since the photos are completely random “popular” flickr photos, even though they purport to be “safe” there are definitely some NSFW photos now and then).

Some thoughts: Qurl sucks as far as response time. I had to limit my photos to five because Qurl was so darn slow responding to my requests and there is no way to do a batch request. BAD. What would I do to fix this? How about dump Qurl entirely. Flickr has their own shortening algorithm that doesn’t even require an API call. If I had to keep using Qurl? I’d go ahead and load the photos to the page for the user with the long links, then I’d make a button on the photo (or link or something) that allowed them to request a shortened URL from Qurl. They click the button/link and an AJAX request fires off grabbing the URL and giving it to them.

I couldn’t get the Flickr API to return only a certain number of Photos. I did everything I could find that it said I should do to get it to only return five or ten photos, but alas, it didn’t work. So I had to make a loop that just used the first five/ten photos and ignore the rest. If it weren’t for Qurl, which takes over 30 seconds most times to shorten 5 urls, I wouldn’t care how many Flickr sent back. Still weird and wasteful and if I had more time I’d look into it until I got it working.

When I removed Qurl from the loop, the photos returned in less than five seconds flat (awesome!). However, with Qurl the response time ranges from 30s to 90s. So AS SOON AS I get the response back I fire off another request. If the response only took 5s total, I’d put a timeout or interval or something that queried only once a minute or so. Or, better yet, I’d make it fire off the request 10 seconds before my photo scroll ended and just put the new photos above my current scroll and make the scroll seem endless (like the pandas).

I spent far too much time on the library. I had big plans and it turned out I wrote way more code than I ended up needing because I was doing VERY LITTLE DOM manipulation. Of course if I worked on this for another forty hours or so the library really would have paid off because it would have saved me time as my interactions got more and more complex. If I had come up with the full design before I started writing the code I would have known I wasn’t going to need much DOM interaction, but as it stands I didn’t have any idea what the page was going to look like until I was almost completely finished with the cQuery JS library.

Queue. Something interesting I came up with was a way of handling mutliple simultaneous AJAX requests and multiple simultaneous animations. A queue.

For the AJAX requests I had an AJAX queue that just held all of my requests (didn’t end up needing this, but it is there if I decide to do the Qurl thing separate from the photo retrieval). I hope to go into the AJAX queue in more detail in another post, but the reason I needed it was the callback function. I needed somewhere to put it until the request completed.

For the animation queue, I didn’t want to set up a whole bunch of different “set intervals” or “set timeouts” so instead I made an “animations” array and then made ONE setInterval that called a function that looped through the animation array. Each spot in the array held an “animation” Object, which had an “animate()” function. The animate function would get called on the object and be allowed to run in the proper context (with “this” functioning as expected). This ended up saving me a lot of code and headaches and made my JS run way faster than it otherwise would have. Of course I ended up only having one animation run at a time and I have no standard way of removing from the queue, but I could add that to the library and there is definitely room for more animations.

One last thing, the song is from Jonathan Neal (who is hilarious). I converted it to .ogg format because Firefox didn’t allow anything else, however it appears that Safari doesn’t accept .ogg format, so if I had more time I’d make something to detect with browser I’m in and respond with the .mp3 format instead…

TOTW: Modernizr

Monday, June 21st, 2010

Modernizr is a JavaScript library that you include on your page that executes itself and adds a series of classes to your HTML tag. This allows to implement modern CSS functionality without worrying about writing conditionals in JavaScript or anything complicated like that. You simply write one (or at most two) style definitions around the functionality you want, like this:

.functionalityYouWant #myElement{
css3thing: blah;
}

.no-functionalityYouWant #myElement{
oldSchoolWay: blah;
}

So, real world example:

.cssgradients .sideNavTitleBox{
background: -moz-linear-gradient(center bottom, #000 13%, #353535 84%);
background: -webkit-gradient(linear, center bottom, center top, color-stop(0.13, #000), color-stop(0.84, #353535));
}

.no-cssgradients .sideNavTitleBox{
background-color: #000;
background-image: url(/media/images/backgrounds/left_nav_box_header.gif);
background-repeat: repeat-x;
background-position: left top;
}

Again, you don’t have to write any javascript at all, you just include the library on the page and it runs all on it’s own and enables this awesomeness!

EDIT: Oh look, ALA just posted a great article on modernizr.

Flurl – Part 1.a: Rolling your own JavaScript library, setting up the core

Monday, June 21st, 2010

Flurl is a mashup I did recently as a practice exercise.

It takes a flickr panda photo-stream, displays a photo, and uses qurl to make a shortened URL link to the photo.

These are the notes I took while I was doing it.

The project can be found on GitHub at http://github.com/cmcculloh/Flurl

For this exercise I didn’t want to use any JavaScript library. Normally I’d use jQuery (naturally) but I wanted to feel the pain of plain jane JavaScript again since it had been well over a year since I had done any AJAX without a library.

I decided I’d roll my own library that I could use to encapsulate the AJAX and DOM selection framework to keep it seperate from the actual app and to simplify my life in actually writing the app.

Since I wanted my library to feel a little jQuerish I decided as an homage I’d name it cQuery and use the _ instead of the $.

Step 1, the ubiquitous self executing anonymous function:

(function(window, document, undefined){})(this, document);

I’ll break it down. The starting paren “(” and it’s mate are just a “cool guy” coding convention to let people know, “this is weird! This is a library! This ain’t yo mama’s JavaScript!”. It’s the same as this:

function(window, document, undefined){}(this, document);

Which is simply just a function that immediately calls itself. The main reason to do this is to prepare our code for minification. When we minify, it will end up something like this:

(function(A,B,C){})(this, document);

So anywhere in our library where we had “window” or “document” or “undefined” it will not be the much shorter “A”, “B” or “C”, much smaller!

Paul Irish explains this in a *little* more detail in his 10 Things I Learned From the jQuery Source video.

Next we build the core function of our library, add it to the namespace and give it it’s “_” shortcut:

var cQuery = function(elm){
};

window.cQuery = window._ = cQuery;

Note that if we didn’t do that last line ‘cQuery’ would not be available in the rest of our JavaScript since it is hidden away inside of the closure we talked about above.

I really like the way jQuery works, and I want my library to mimic this. So calling:

cQuery("#domElementById").someMethod().anotherMethod();

ought to work.

Functions in JavaScript are just Objects that you can invoke. Functions can have their own methods, properties, etc. So basically cQuery is just an object that can DO something on it’s own so we can say cQuery() instead of cQuery.doThing(). Much more convenient. So basically our var cQuery = function(elm){} code is just setting up my cQuery library object in a way that it can be called and passed the dom element we are working with.

Since I want to be able to “chain” things in my library, I’ll need to add the methods in there that enable my chaining. I do this by ending my cQuery function with a return statement that returns an object containing the methods I wish to be available for chaining, each of these methods in turn returning an instance of the cQuery object (unless the method is specifically supposed to return something else, which makes it a destructive method because it ends my chaining), like in this example:

(function(){
var c = function(){
return{
blah:function(){
alert("blah");
return c();
},
blah2:function(){
alert("blah2");
return c();
}
};
};

window.c = c;
})();

c().blah().blah2();

That’s it! Now I’ve got the core of my JavaScript library all set up, chaining enabled, library closed in but available on the namespace, all ready to be made useful! Here’s the code we’ve got so far:

(function(window, document, undefined) {

var cQuery = function(elm){
//DOM selection and storage will go here

return {
//chain-able library methods go here
};
};

//make sure our library is exposed to the global namespace and make a shortcut “_” so we don’t have to type cQuery every time.
window.cQuery = window._ = cQuery;

})(this, document);