Archive for December, 2008

New CNET/CBS iPhone App Totally Sucks

December 17th, 2008 by Aaron N.

As many of you know I used to work at CNET. It’s been over a year since I left but I occasionally still work on things there and I still care about how things go there (and I still like CNET – it was a great place to work). But when I see the company do stupid things it still burns (just as it did when I worked there).

In the spring of this year a coworker there released a JavaScript UI interface for mobile apps for the iPhone (CiUI). This script lets you turn a mobile interface into an animated, ajax driven experience without writing any of your own JavaScript and it’s very tiny. I use it for Iminta’s mobile interface and it saved me a ton of time.

For some reason, the iPhone experience that they released 9 months ago is gone and what’s there now totally sucks. Read the rest of this entry »

TOC for Expanded MooTools Book – Feedback?

December 12th, 2008 by Aaron N.

As I mentioned in a previous post, Apress wants me to embiggen the MooTools book. It’s worth noting that the previous book was on their “firstPress” label which is intended to introduce people to an emerging technology. These books are supposed to be about 150 pages long (and I’ll point out the font for this imprint is HUGE). When I was about 50-60% through writing my book, I’d already passed that threshold and my deadline was rapidly approaching. Consequently, by the time I got to the good stuff – robust code examples of how to write classes and that sort of thing – I had to stop writing and hand it off to the publisher.

The book has done relatively well for their “firstPress” imprint – and, to be clear, I have no idea how many even sold. Apress says the few indicators they have imply that it’s doing well. Let’s just say the publishing industry doesn’t exactly give or generate real-time stats. Anyway, they asked me to go back and finish what I wanted to and add several more chapters to the book. The full Apress imprint means things like, oh, a font size aimed at readers under 60 and an index, which the current book suffers without.

I’ve been contemplating what I’d like to add to the book and decided that I want to answer a different question. The current book answers the question, “What is MooTools and how does one use it?” What I’d like to answer in this addition is, “How do I – me, Aaron Newton – use MooTools and what have I learned by writing a lot of code with it?” I want to share the ins and outs of how I build web sites that are heavily scripted.

So, with that in mind, here’s my current TOC draft. I’d greatly appreciate any input. What chapters here would you skim through or skip entirely? Which chapters would you be eager to turn to first?

I’ll note that the first section of chapters – those without descriptions – are basically the current book’s contents. I invision most of the new chapters as being relatively brief with a few exceptions.

Part 1: Learning MooTools
Chapter 1: Getting Started with MooTools
Chapter 2: Reviewing MooTools
Chapter 3: Shortcuts and Helpful Functions
Chapter 4: Native Objects
Chapter 5: Elements
Chapter 6: Utilities
Chapter 7: Classes and Inheritance
Chapter 8: Getting Started with Classes
Chapter 9: Fx
Chapter 10: Request
Chapter 11: Plug-Ins
Chapter 12: Third-Party Plug-Ins

Part 2: Using MooTools – Real World Examples
Chapter 13: A Simple Use Case
This is what Chapter 13 is now; an example of a simple page with a simple use case. From this we turn our use case into a class in chapters 14, 15, and 16.
Chapter 14: When to Write Classes – Programming to Patterns
This covers my perspective on when to write classes (i.e. as often as possible) and why.
Chapter 15: Writing a Tab Class
Applying the thinking from Chapter 14, we turn the use case in Chapter 13 into a class (this is basically what chapter 14 is now)
Chapter 16: Writing Flexible Classes
Here I cover extending classes (currently chapter 15).
Chapter 17: Using Mixin Classes with Implements
A deeper look at using Implements and how you can use it to reduce your work.
Chapter 18: Best Practices for Classes
Do’s and don’ts for classes.
Chapter 19: Integrating Your Work with MooTools
Extending MooTools classes (without altering the actual MooTools codebase) to add functionality to them.
Chapter 20: Extending Native Objects
When, in your own codebase, it makes sense to extend String, Element, etc.
Chapter 20: Avoiding Repetition
How to let your code do your work for you.
Chapter 21: Organizing and Extending Numerous Classes
Managing families of classes.
Chapter 22: Controller Classes and Global Methods
Writing super classes and static methods that will control numerous instances of other classes.
Chapter 23: Hidden Secrets in MooTools
Undocumented functionality in MooTools that you can tap into.
Chapter 24: Debugging and Solving Problems
Using Firebug and other debugging techniques to figure out what’s not working.
Chapter 25: Test Driven Development
Writing code against unit tests and their benefits.

Part 3: Building Your Site Well
Chapter 26: Organizing Your Site’s Code
Namespacing, anonymous closures, and other methods for making your codebase easier to manage.
Chapter 27: Defining The Defaults For Your Environment
Overwriting the default options and states for existing classes in your environment.
Chapter 28: Unobtrusive Designs
How to build your site to work when JavaScript doesn’t.
Chapter 29: Ajax Inside and Out
A closer look at writing applications that make heavy use of Ajax.
Chapter 30: Things to Avoid
A laundry list of pitfalls and tempting design patterns that can get you into trouble.

Chapter 31: Where to Learn More
This is what is currently in chapter 16.

Appendix: Core Concepts in JavaScript

MooTools Class Mutators

December 11th, 2008 by Aaron N.

This question got posted to the MooTools User Group earlier and I thought I’d answer it here because it’s pretty interesting:

Hey everyone,

I just discovered Mutators today when I was digging through the mootools source. I’ve been using mootools for a couple of years now and am pretty familiar with some of it’s internals. Coming from a heavy OOP background I found the whole extending and implementing classes in JS very interesting. So, what I’m really wondering is this:

What is the formal definition of Mutators?

What are the pros and cons of developing them to add features to JS classes?

Thanks,
Brandon

Mutators take properties of classes and process them in specific ways. MooTools only ships with the two: Extends and Implements. It’s important to note that mutators are applied to the class when you invoke new Class, so they can only be used to alter classes themselves, not instances of them. So, for instance, Extends is processed when you do this:

var Widget = new Class({
Extends: Foo,
initialize: function(){ ...}
});

Extends creates a new instance of Foo and mixes it in to Widget. Unlike the initialize method, which is used when you invoke new Widget.

In general, mutators are useful when you want to run functionality when classes are created, but not when they are instantiated. This low level access lets you change the way classes themselves work, but only in this very specific way. The only mutator that I’ve ever actually found a user for was the Class.Binds mutator (which I originally got from Jan), which let you do this:

var Widget = new Class({
binds: ['click'],
initialize: function(el) {
el.addEvent('click', this.click);
},
click: function(){
console.log(this);
}
});

Normally the console statement above would log el because this by default points to the element unless you use Function.bind to make sure the method is bound to your instance of the class.

The Class.Binds mutator let you specify a property (binds) that listed the names of methods that should automatically be bound to the instance of the class.

MooTools 1.2 reworked how classes work a bit (mostly to work around Opera bugs and the like). Consequently mutators like Class.Binds won’t work, because they need to be applied when you invoke the class (new Widget) in order to bind them to that instance. I have a Class.Binds replacement that runs at runtime that almost duplicates this entirely (you have to call a method to bind the classes).

MooTools 1.3 should include a post-instantiation mutator that will let me make Class.Binds work the way it used to (i.e. invoke a method when the class instance is created).

The only other mutators that I’m aware of were authored by Nathan White that were designed to create Singletons and to help with the creation of private methods through closures.

Update: Nathan points out that there’s also an Exposes mutator that Jan put together.

FormValidator Gets a Shortcut and a New Option

December 11th, 2008 by Aaron N.

From the I-don’t-know-why-I-didn’t-think-of-this-before department comes two  new goodies for FormValidator.

  1. A new option stopOnFailure that will prevent the form from submitting if the validation fails. Previously you always had to do this yourself  in the onFormValidate event. No more! Note that this is sort of a breaking change in that if you were using FormValidator previously and you let forms that failed submit continue, you’ll run into this new option, as it defaults to true.
  2. Thanks to Element Storage you could always retrieve the validator for a form by doing el.retrieve(‘validator’) but only if you’d created one already. Now you can use Element.get/set to access and create the validator for a form. Additionally, you can call the new Element method validate to get back a true/false value to see if it validates. Note that calling this shortcut will expose the validation errors (just as if the user submitted the form).

Dragging and IE7

December 10th, 2008 by Aaron N.

So I got an email today from someone having issues with dragging things in IE7. They even hit my example in the wikitorial for resizing things and reproduced their trouble. The issue showed up as they tried to drag the handle. Nothing happened until they released (mouseup) the handle and then the object followed the mouse around until they clicked again.

After a bit of work I figured it out: IE thinks you’re trying to drag the image (as if to download it to, say, your desktop). This in turn delays the event for mousedown or something and screws up drag.

The solution is to avoid using images (and probably also links) and instead use a DOM element with a background image instead. IE doesn’t think you’re trying to drag the item at the OS level if it’s just a DOM element.

Thanks again Microsoft, for taking up my time!

Cleaning up CNET references in the code

December 10th, 2008 by Aaron N.

Just a quick FYI. I’ve been cleaning up references to CNET in my codebase now that Clientcide is on its own. So things like setCNETBaseHref is now Clientcide.setAssetLocation – the old method still works though.

I’ve also removed the CNETAPI class as I’m no longer maintaining it and I’m sure the API has changed in the last year (with new functionality). I also added the Clientcide namespace which, at the moment, only contains setAssetLocation and the svn version of your download from the download page.

Given that this was a big code change (a lot of things were touched, if only slightly), please do let me know if you spot anything that’s seems off.

SimpleEditor localizations, FormValidator and XHTML, CiUI release

December 8th, 2008 by Aaron N.

Coupla monday morning goodies for you all. They are:

  • SimpleEditor now can be localized and there are language files for Spanish, French, Portuguese (and English of course). This is all thanks to Miquel Hudin (who has contributed to the localization effort to many of the other classes available here). Thanks Miquel! If you know a language that isn’t represented in our localization efforts, feel free to send us new files.
  • FormValidator uses a custom element property called “validatorProps” to pass in values for validators (like minLength and maxLength). This doesn’t pass XHTML validation and at the prompting of one user I changed this methodology to use css class values instead. See the new docs for FormValidator for examples. The old method still works if you don’t care about validating XHTML.
  • CiUI – CNET’s Iphone interface script – just had a new release after several months of dormancy. Author Vlad Olexa writes on the project page:

    Release notes

    version 0.3

    • New feature: page caching. When you annotate your link with classname cache, i.e. <a href="my_page.htm" class="go_forward cache" title="My Page">My Page</a> the page will be cached. Meaning, navigating to that page again will not fire a new Ajax request.
    • Bug Fixes:
      1. The cancel function now functions properly (ciUI.cancel()) when you’re trying to cancel a loading page
      2. When a new page is requested while the current one is still loading, the current page will stop loading before trying to load the new one. (This was causing unexpected content appear after users navigated away from a page).
      3. Fixed an issue with the back button where it would sometimes be disabled when trying to get to the very first page in the hierarchy.
      4. Checkboxes return correct values in Firefox

Link Nudging

December 5th, 2008 by Aaron N.

David Walsh has a nice little effect featured in the footer of his blog. He writes of it:

Link nudging is the process of adjusting the padding on a link slightly to show a simple, tasteful “jump” when you place your mouse over a link. The effect is magnified when mousing on and off a link quickly.

Here’s the code:

$$('#footer-topics-1 a').each(function(el) {
	var fx = new Fx.Morph(el,{ duration:300, link:'cancel' });
	el.addEvents({
		'mouseenter': function() { fx.start({ 'padding-left': 20 }); },
		'mouseleave': function() { fx.start({ 'padding-left': 0 }); }
	});
}); 

No Sizzle For Moo

December 4th, 2008 by Aaron N.

The MooTools dev team discussed the whole sizzle thing at great length over the past few days. I posted about it quite a bit and there was a lot of talk on the Google Group for MooTools users. I’ll admit that when I first read John Resig’s email I was very open to the idea and thought it was worth considering, though I was a bit conflicted. I posted my thoughts and talked with Valerio and Tom about it at length.

In the end, I felt that it was too early to tell if it was a good idea and thought it best if we waited to see what the Dojo team would do with it.

Then Valerio posted this well-reasoned post about it and completely convinced me that it’s not the right move for MooTools. I agree with him 100% at this point.

There are several reasons why a project like MooTools would never include a third party library like Sizzle in its codebase. First of all, we already have a very fast, very manageable and solid CSS selector engine in place. I worked on it a lot, I know how it works, and I know that if it ever needs a fix, every MooTools collaborator can just git it and fix it, right away. Every Mootools collaborator knows how MooTools works, what our code practices are, and how to submit either a patch (if they don’t have Git credentials), or patching the code themselves.

Read more over on the MooTools Blog »

MooTools on the Server Side

December 4th, 2008 by Aaron N.

Digging around on Nathan White’s blog, I found a link to some MooTools 1.2 plugins over on Chris Esler’s blog. Then I found this little gem.

Well I’ve been rather obsessed lately with getting mootools running in a server-side environment. I’ve had some minimal experience with Helma, but I decided to try and build my own. At this point its rather simple, but I have managed to initiate the jetty webserver with mootools and to write java handlers in javascript (again with mootools) to handle my requests. I looked at a few internet posts about doing your own javascript web frameworks, but most I had a hard time getting to work on my local environment as well as my server environment. So after about a month of diving into Rhino documentation and Jetty documentation I’ve got something fairly predictable working.

Nifty.