Archive for the ‘Javascript’ Category

July 2, 2010 Off

First Thing’s First: Validate

CakePHP Javascript PHP

Have a sweet function written that requires valid variables? Make sure you’re validating those variables at the top of your function. Validating variables before (as apposed to while) they are used is key in making sure that important logic isn’t executed using crappy data.

Here’s an example of validating first, then executing:

	/* The good... */
	public function insertNewInvoiceItem($item, $quantity)
	{
		if(!$item instanceof Shop_Item) {
			throw new InvalidArgumentException ('Expecting an item to be an instance of Shop_Item.');
		}

		if(!is_numeric($quantity)) {
			throw new InvalidArgumentException('Quantity is not numeric.');
		}

		// We're good to go. Continue with the function...
	}

The method above is also much cleaner than if... else... statements and limits the extra (perhaps useless) work required by your code:

	/* The bad... */
	public function insertNewInvoiceItem($item, $quantity)
	{
		if($item instanceof Shop_Item) {
			// Do some $item stuff...

			if(is_numeric($quantity)) {
				// Do some $quantity calculations
			} else {
				// Uh oh, $quantity was bad but we've
				// run some Shop_Item logic already.
			}
		}
		else
		{
			// $item is not part of Shop_Item
		}
	}

I used a simple example to demonstrate my point but when this technique is used on more complex logic, it’s benefits are much more obvious.

Happy coding!


July 1, 2010 5

CSS & JS Coding Standards / Organization [OOP]

CSS Javascript

By no means should this article serve as the bible of CSS & JS coding standards, though it should get the ball rolling for those who are unsure where to start. Over the years, I’ve had the privilege of working with many talented developers, each of whom have left little bits and pieces of their coding style behind. From those pieces I’ve assembled this article.

Let’s begin…

Alphabetize Your Code

Alphabetizing your code is an extra organizational step often overlooked. It’s especially helpful in PHP classes containing getter and setter functions, but it also comes in handy for CSS and JS. Often times similarly named elements will be somewhat related in function, so clustering them together makes them easier to find.

I’d recommend alphabetizing everything from JS class and function names, to CSS classes and ID’s. You’ll notice that every code example in this article is alphabetized to further exemplify the point.

Read the rest of this entry »


May 31, 2008 3

TinyMCE & Mootools Ajax.Form Class – Feel The Love

Javascript

TinyMCE (Tiny) is regarded as one of the best open source, HTML WYSIWYG editors of our time – it’s powerful, relatively easy for browsers to digest, and free to use. The frustrations of using Tiny arise when you attempt to submit your Tiny-enhanced form via Ajax. Because your Tiny textarea inputs are converted to a mess of divs, spans, iframes, etc, Mootools Ajax.Form class fails to recognize the textarea. Fear not, there’s an easy workaround.

Read the rest of this entry »

Tags: , , ,


January 23, 2008 34

21 Must-Have Scripts For Your Favorite Javascript Library (jQuery, MooTools, Prototype)

Javascript

In attempting to spruce up my knowledge of Javascript, I’ve been trolling around the net collecting scripts and tutorials for various Javascript libraries, mainly jQuery, MooTools, and Prototype. Of the three, MooTools seems to supply me with the most JS lovin’ – it’s easy to learn, holds super powerful Ajax abilities, and is customizable to the core.

After sorting through tons of scripts, I thought I’d share the best ones with you.

Read the rest of this entry »

Tags: , , , , , , ,


December 21, 2007 Off

Updates & Checkboxes & Loadbars! Oh My!

CSS Inspiration Javascript Labs

Yep. I’m mixing programming references with The Wizard of Oz. Shut up.

The all-too-awesome PHPMailer class has been updated and is awaiting your download.

If you use MooTools (or even if you don’t), Vacuous Virtuoso’s Fancy Form is totally worth checking out. It’s a mix of CSS and Javascript that enables you to create custom form checkboxes. There are other script on the net that claim to do the same thing, but none are as cross-browser compatible.

Lastly, to add a splash of color to your ajax load processes, check out Bram Van Damme’s Progress Bar Handler. It’s probably the smoothest, best looking non-flash based progress bar on the net.


November 28, 2007 Off

Control Suite, High Quality Controls & Widgets for Prototype.js

Javascript

Ryan Johnson, the founder of LivePipe, created one of the best prototype-based widgets I’ve ever used.

Control Suite is a collection of six high quality widgets and controls for Web 2.0 applications built using the Prototype JavaScript framework. Each script is well tested, highly extensible, fully documented and degrades gracefully for non JavaScript enabled browsers where possible. All scripts are MIT licensed and are thus completely free for any purpose in any project.

Thus far, the SelectMultiple is finding some serious use in my web applications. Assuming I come up with something not already discussed in Ryan’s examples, I’ll post it in Labs.

Go to the official Control Suite website.