BKWLD

Author Archive

How to refresh listeners in Prototype

By Garrett on June 15, 2008 at 12:11 am

Lets say, you are making a site, were everything is AJAX, and you rely on event handlers to know when people double click on this element, drag the image across the site, etc. Normally you would start listening to things once the DOM fires (when the page loads), but problem is, it only fires once. Here is an example of a normal “onload” listener in Prototype.js:

Event.observe(window, 'load', function () {
	$$('tr[rel="file"]').each(function(element) {
		element.observe('click', function(event) {

			new Ajax.Request('file_info.php', {
				asynchronous: true,
				method: 'get',
				parameters: 'ajax=true',
				onComplete: function(t) {
					$('files').update(t.responseText);
				}
			});
			event.stop();
		});
	});
});

So, once the window loads, tell every table row with the rel attribute named “file” to some things once its clicked (in this case, gets the file info). Problem is, with all the new table rows coming in, Prototype wasn’t told to listen to the new table rows, only the previous ones that are gone.

I spent about an hour trying to figure out how to re-load the observe above. With tension building, I finally found something in the Prototype API documentation. You can establish your own psuedo type observation, which you would call when the window loads! Here is an example:

document.observe('start:files', function() {

	$$('tr[rel="file"]').each(function(element) {
		element.observe('click', function(event) {

			new Ajax.Request('file_info.php', {
				asynchronous: true,
				method: 'get',
				parameters: 'ajax=true',
				onComplete: function(t) {
					$('files').update(t.responseText);
					document.fire('start:files');
				}
			});
			event.stop();
		});
	});

});

Event.observe(window, 'load', function (event) {
	document.fire('start:files');
});

So when the window loads, it will load another observer. Although this seems redundant, it’s the only way to re-fire the listener when you need to. Let me note, that the start:files you can name it anything you want really, although it should have a : somewhere in-between it.

Styling List Items Seperately

By Garrett on June 13, 2008 at 8:11 pm

I was wrapping up on a project, and the time came where the color on the lists (1, 2, 3) needed to be different colors than the actual <li> inside needed to be. The only solution is to wrap any content inside the <li> with some sort of tag to target the text to change the color. Although that has no problem at all, what if this needs to be site wide? It will be very cumbersome to remember, let alone do it to every list you create.

I decided to use JavaScript to search for every <ol> and for every <li> inside of it, then it goes ahead and wraps it in the selector that changes it back to default color. In this case its a <span>.

This is what it does, this will be the before HTML:

<ol>
   <li>Item text goes here</li>
   <li>Item text goes here</li>
   <li>Item text goes here</li>
   <li>Item text goes here</li>
</ol>

And, after the JavaScript does its thing:

<ol>
   <li><span>Item text goes here</span></li>
   <li><span>Item text goes here</span></li>
   <li><span>Item text goes here</span></li>
   <li><span>Item text goes here</span></li>
</ol>

Here is the JavaScript:

window.onload = function () {
   var li = document.getElementsByTagName('ol');
   for(i=0;i<li.length;i++) {
      if(li[i].childNodes[1].nodeName == 'LI') {
         var li_count = li[i].childNodes.length;
         for(b=0;b<li_count;b++) {
            li[i].childNodes[b].innerHTML = '<span>'+li[i].childNodes[b].innerHTML+'</span>';
         }
      }
   }
};

Here is the CSS:

.container ol {
	padding: 8px 10px 8px 25px;
	list-style-type: decimal;
	color: #c19203;
	font-weight: bold;
	}
	.container li {
		padding: 4px 0;
		}
	.container ol span {
		font-weight: normal;
		color: #333;
		}

How Garrett Does Email

By Garrett on May 30, 2008 at 2:24 pm

Continuing on what Mark started, I will explain how I do my email.

Mail is usually always open whenever I am here at the office, at home doing my own things, or on the iPhone. It’s hard to not be able to reach me. To be able to do this efficiently though, I setup IMAP on my email accounts rather than POP. IMAP allows me to be able to read a email on the go and mark anything I read as read, so later on in the evening everything I just read is marked as read so I don’t have to go through them all again. Same goes with deleting.

If I ever see a email in there, it bugs me not to look at it. I don’t do anything special with organizing my email, although I probably should. I just have a inbox for each of my accounts; BKWLD, .Mac, and my personal email.

One thing I tend not to do is delete email (other than spam of course), if I ever need to go back and look through my email I can. Although now with Time Machine, I can probably get away with it. If you have any suggestions, let me know.

Secure Your Web Apps

By Garrett on February 28, 2008 at 4:56 pm

Facebook back in August, let super secret code out. Which made its rounds on the internet before it has now died down. Due to the fact that, a extremely large social network, let out it’s source code opened the alley for media to scrutinize Facebook.

Due to the code being leaked, a PHP developer wrote a article about securing your site, making sure no valuable code is leaked.

I suggest reading up on this, as any practice that secures your code, is a practice you should know.