Archive for December, 2008

Why You Should Consider Releasing Code

December 31st, 2008 by Aaron N.

I posted the other day about Ian Collins’ Moo-ish Template, a scaffold for putting together your own JavaScript library organized as MooTools does (and as I do for Clientcide). But let me tell you why I think you should.
Read the rest of this entry »

Event Delegation, take 2

December 30th, 2008 by Aaron N.

Last week I released (prematurely) a MooTools plugin called Event.Delegate that added a new method to elements that allowed you to delegate a method to a parent. Read the rest of this entry »

New Slideshow Plugin – Horizontal Carousel Version

December 29th, 2008 by Aaron N.

Got an email asking how hard it would be to add a horizontal scrolling transition to my SimpleSlideShow class. At first I was going to suggest that the inquirer simple use my IconMenu class, but it’s always nagged me that for a carousel that has only one item visible at a time, IconMenu is overkill. It’s a big class with a ton of logic dedicated to figuring out pagination of several objects. You just don’t need all that if you’re only showing one at a time.

So what the heck, I just went ahead and added the horizontal scroller to the class as an extension in about 60 lines of code. Not bad.

You can see it in action in the wikitorial – there’s also a carousel version of SimpleImageSlideShow – and the docs, of course.

The Difference Between Element.set/get and Element.store/retrieve

December 29th, 2008 by Aaron N.

Posted today in the MooTools user group:

Hi!

after a lot of mootools programming and docs studies, I still have a
little question:

when should I use get/set and when store/retrieve? Or, in other words,
what’s the best use for get/set and what’s the best for store/
retrieve?

Element.get/set are used for element properties (img src, element class, input value, etc) as well as custom values that include special properties (like ‘html’ and ‘text’ as well as ’styles’ and ‘events’) and default properties for built-in classes (el.set(‘tween’, options)). Element.set/get almost always accepts and returns strings and objects including the built-in instances of classes (which are also objects) – el.get(‘tween’) returns the built-in instance of Fx.Tween.

Examples:

//setting properties of elements
input.set('value', 'foo');
checkbox.set('checked', true);
myDiv.set('html', '<b>bold!</b>');
myImg.set('src', url);
//set can also take an object
myDiv.set({
  styles: { width: 100, height: 100}
  events: {
    click: function(){ alert('you clicked me!'); }
  }
});

//using custom setters/getters
element.set('tween', {duration: 500});
element.get('tween').cancel();

Storage is for storing arbitrary values for an element. These values may be any object type – boolean, array, object, function, class, etc. You can store any data and retrieve any data and not worry about memory leaks (if you stored these things as properties of the elements you would risk creating circular references, which are the main causes of such leaks.

Examples:

//storing misc. data
myImg.store('mouseOverImg', alternateSrcUrl);
myGalleryDiv.store('galleryImgs', $$('img.slide'));
//storing pointers to instances of classes
var myValidator = new FormValidator(myForm);
myForm.store('myFormValidator', myValidator);

You can define custom setters and getters for ‘default’ instances of classes or for other logic as you like. The benefit here is that when you call el.get(‘tween’) until that moment the element doesn’t have a default instance of Fx.Tween. It’s only when you use the getter or setter that it is created.

Example from Fx.Tween:

Element.Properties.tween = {
	set: function(options){
		//get the instance of tween if there is one already
		var tween = this.retrieve('tween');
		//cancel it's operation if it's running
		if (tween) tween.cancel();
		//store the new options and return the element
		//note that element.set always returns the element
		return this.eliminate('tween').store('tween:options', $extend({link: 'cancel'}, options));
	},
	get: function(options){
		//if options were defined with the get instruction, store them
		if (options || !this.retrieve('tween')){
			//store the options
			if (options || !this.retrieve('tween:options')) this.set('tween', options);
			//create a new instance with those options
			this.store('tween', new Fx.Tween(this, this.retrieve('tween:options')));
		}
		//return the instance
		return this.retrieve('tween');
	}
};

Finally, you can implement methods into the Element prototype to reference these default values (element.tween(‘opacity’, 0) and element.fade(‘out’) both call element.get(‘tween’) to execute their logic).

Again, from Fx.Tween:

Element.implement({
	tween: function(property, from, to){
		//get the instance of tween and start it
		this.get('tween').start(arguments);
		//return the element
		return this;
	}
});

Mooish Repository Template and an All-JsonP Showcase

December 28th, 2008 by Aaron N.

Ian Collins is apparently hooked on MooTools because he sure has been writing a whole heckuva lot of it. He emailed me a few days back about his site that is all dynamically built off Twitter, Flickr, and other sources using JsonP to fetch all the data. The entire experience is built in JavaScript. Looking at the source of his site you find this lovely little easter egg:

<body>
  < -- I win at SEO -->
</body>

The source of the site itself is all cleanly written MooTools classes, reminding me of my thoughts on programming to patterns.

He’s also released through github a project structure based on MooTools and Clientcide tools and conventions. This is basically how I organize all the Clientcide code along with both test frameworks (Ian includes ScrewUnit – while Clientcide and MooTools use JSSpec, but they both do the same thing – and my Unit Test Framework). Read the rest of this entry »

Thoughts on Element Delegation

December 24th, 2008 by Aaron N.

Yesterday I released a bit of code that allowed you to delegate events to their parents. This pattern isn’t a new one in JavaScript, but it wasn’t something natively supported in MooTools in any sort of easy interface. You could do it, but you had to write a lot of it yourself.

My version worked like this:

myElement.delegate(test, event, fn);

Read the rest of this entry »

Removing Delegation

December 24th, 2008 by Aaron N.

It’s xmas eve and I don’t have time to explain it, but I will later tonight or tomorrow. I’m removing Element.Delegate from my repository for now. Basically, it was premature. It just wasn’t ready for release and I don’t have time to fix it. I’ll revisit it in the next few days.

Event Delegation for MooTools

December 23rd, 2008 by Aaron N.

I just posted a long post about a bunch of new plugins I released today. Lots of good stuff in there – pointy tips, a form validator extension, a tips extension, but I want to take a minute to talk about Event.Delegate.js.

Event delegation is a common practice where by you attach an event listener to a parent object to monitor its children rather than attach events to all the children. It’s far more efficient when you have numerous items on a page that you want to interact with. For instance, let’s say you want to monitor all the links on a page. Using MooTool’s addEvent method you could loop through all the link as and attach an event to each:
Read the rest of this entry »

Loads of new Plugins: PointyTips, FormValidator.Tips, Event.Delegation and more

December 23rd, 2008 by Aaron N.

I’ve been a busy bee. Today I released numerous new plugins and scripts for your holiday enjoyment.

Pointy Things

First up is an extension to StickyWin.ui that gives you a popup layout with a pointer. There’s a lot more about this in the wikitorials, but here’s a quick overview:

  • You can point the pointer in any direction around the tip. The directions map to the hours on a clock face, so 12 is pointing up in the middle, 1 is pointing up, but on the right, and on around the tip.
  • There are two default themes, a dark one and a light one.
  • The ui can be used to quickly generate the DOM wrapper for use in anything, but there’s also a new StickyWin class that makes it easy to drop a tip into the page next to an element, pointing at that element from the left, right, or from above or below.
  • There’s an extension to the Tips class (Tips.Pointy) that works just like the Tips class but has a pointer.
  • There’s an extension to the FormValidator class (FormValidator.Tips) that uses this to point out errors in the form in a pointy popup.

Read the rest of this entry »

POST with JsonP

December 18th, 2008 by Aaron N.

For those of you unfamiliar with JsonP, it’s the process of using script injection to fetch data across domains (where Ajax requests prevent this access). You inject a script tag into your document with the url of a web service and instruct that web service to send JSON data back wrapped in a function name you specify in the url. Then you have the function defined in your document waiting for that script to load and call your function, passing the response.

You can see it in action here using my JsonP class for MooTools.

Today I got an email from a fellow by the name of Alan who has released jsonptunnel – CGI and FastCGI scripts that receive JSONP requests (which, do to their script call methodology are always GET requests) and turn them into POST requests. Tres cool.

In order to POST to an off site web service, a GET URL is formatted and fetched that looks like this:
?extURL=http://dipert.org/alan/calc.php&extMethod=POST&num1=10….

This tells the CGI to fetch the specified URL using the specified HTTP verb and pass the specified variables.