Posts Tagged ‘mootools’
Links^2 – December 13th, 2010
Monday, December 13th, 2010- MooTools 1.3 by Ryan Florence – A comprehensive guide
- Maki – A free web design mock-up tool for pixel perfect layouts
- CSS Animations – A transition framework that utilizes CSS3 animation abilities
- ImageOptim – Optimizes images so they take up less disk space and load faster by finding best compression technique
- CSS: CamelCase Seriously Sucks! – Well put
- What’s new in JavaScript 1.8.5 – Well, mostly to be used under Server Side like Node.js
JavaScript Tip: Remove ‘falsy’ Items Out of an Array
Monday, November 1st, 2010The new ECMAScript 5 brought a lot of enhancements that we used to see only on JavaScript frameworks. The Array class now has methods like forEach, map & filter, which are very useful.
If you need to support older browsers, which you probably do, this filtering method can be also found in MooTools (Array#filter), jQuery ($.grep) and other JS frameworks.
A quick tip to remove all falsy (false, null, undefined, 0, NaN or an empty string) items out of an array:
var a=[1,2,"b",0,{},"",NaN,3,undefined,null,5];
var b=a.filter(Boolean); // [1,2,"b",{},3,5]
Since Boolean constructor is also a function, it returns either true for ‘truthy’ argument or false for ‘falsy’ argument.
For example:
Boolean(0); // false
Boolean(true); // true
Boolean(1); // true
Boolean(""); // false
Boolean("false"); // true. "false" is a regular, non-empty string
And writing
b=a.filter(Boolean);
is actually the same as writing:
b=a.filter(function (x) { return Boolean(x); });
Links^2 – October 5th, 2010
Tuesday, October 5th, 2010- umbrUI – Power of CSS3 (WebKit only). Custom checkboxes.
- jQuery 1.4, MooTools 1.2 Compared – Great comprehensive comparison between the two libraries by Ryan Florence
- Background Experiment – Cool experiment with background positions
- ProCSSor – Reformat and beautify CSS Code. Neat UI.
- Using Google Docs as an Online Database – .NET library
- HyperDock – Awesome improvements for OS X’s dock, like Win7′s: window previews, maximize a window by dragging it to the top and more goodies.
MooTools toElement Method and ToElement Mixin
Tuesday, September 28th, 2010The toElement Method
Classes like Calendar, Slider, AutoCompleter etc. are all containing elements or responsible for elements.
There are several approaches for how to handle the element in the class:
- Pass a container element as an argument to the constructor of the class, and append to it new elements that the class creates
- Have the actual behavior-less element(s) on the HTML and pass them to the class constructor as an argument, the class will apply its behaviors on them
- Create all elements in the class and have them available for appending
Each approach is good for a different situation, but the toElement method is meant mostly for the last one.
Example of a plain class, and how its element is being appended to the document:
var AutoCompleter=new Class({
initialize:function () {
this.input=new Element("input");
// autocomplete magic
}
});
var autoCompleter=new AutoCompleter();
$("container").grab(autoCompleter.input);
This is quite ugly to use the public property input, which its name can be changed someday and then the code breaks. Moreover, in this way, each class declares its own property and this can lead to inconsistency between the property names.
This is where the toElement method comes in handy: it’s a MooTools convention for a method that simply returns the element.
var AutoCompleter=new Class({
initialize:function () {
this.input=new Element("input");
// autocomplete magic
},
toElement:function () {
return this.input;
}
});
var autoCompleter=new AutoCompleter();
$("container").grab(autoCompleter.toElement());
But wait, there’s more: MooTools $ / document.id function knows that if it gets an object that has the toElement method, the method should be invoked and the returned element will be returned out of the document.id function. Luckily, all of the MooTools Element methods use document.id inside them, so, for instance, when grab or adopt are called with any object as an argument, it will be passed thru the document.id function which will translate it to the toElement result. Hence, we can use this:
$("container").grab(autoCompleter);
The ToElement Mixin
The ToElement Mixin is supposed to make the toElement method even simpler, and make it lazy-load the element, i.e. create it only when needed.
var ToElement=new Class({
ui:{},
toElement$ensureElement:function () {
var element=this.toElement$getElement();
if (!element && this.build) this.build();
},
toElement$getElement:function () {
return (this.ui ? this.ui.element : this.element) || this.element;
},
toElement:function () {
this.toElement$ensureElement();
return this.toElement$getElement();
}
});
Usage:
var AutoCompleter=new Class({
Implements:[ToElement],
initialize:function () {
},
build:function () {
this.ui.element=new Element("input");
// autocomplete magic
}
});
var autoCompleter=new AutoCompleter();
// element isn't ready yet
// the grab calls document.id which calls toElement, where the element is prepared
$("container").grab(autoCompleter);
First, it adds a ui hash to every class instance. That’s my convention to save elements in classes of this kind. Second, it adds a toElement method, which is responsible of calling the build method if exists, and return this.ui.element or this.element out of the instance.
Benefits are:
- Lazy load the element – it’ll be created only when the document asks for it
uihash to contain all other elements, and abuildmethod as a simple convention
Links^2 – July 22th, 2010
Thursday, July 22nd, 2010- Def.js – Simple Ruby-style inheritance for JavaScript. The use of “<<” operator with “valueOf” for inheritance is very creative.
- CSS3 Pie – Makes Internet Explorer 6-8 capable of rendering several of the most useful CSS3 decoration features.
- There’s a class for this - Advanced MooTools Class tutorial.
- Microsoft WebMatrix – Microsoft’s new IDE for rapid web development, using the new Razor Engine (.cshtml). Looks good at first sight.
- 10 Things I Learned from the jQuery Source – Advanced JavaScript screencast. Worth the ~54 minutes.
- HTML5 Essentials Slideshow – More on the HTML5′s new features.
- CSS3 Slideshow