Here’s a step-by-step and line-by-line example of how to write a Mootools Class. I wrote this example for a javascript class I taught here at CNET for our developers and figured I’d share with everyone else.
Archive for the ‘Examples’ Category
Latest mootools tutorial: how to write a Class
June 25th, 2007 by Aaron N.Snook on Private Methods
June 19th, 2007 by Aaron N.Jonathan Snook has a nice write-up about using private methods in javascript.
With JavaScript, you can create private methods and properties using what Yahoo describes as the module pattern. Here’s the basic construct, including a private method:
MyObject = function(){ var privateMethod = function(){ /* do stuff */ }; var obj = { publicProperty:5, publicMethod:function(){ /* do stuff */ }; }; return obj; }(); // run it right awayIf you’re not familiar with this pattern, it’s really quite cool. It takes advantage of closures, allowing the public methods to access the private methods. I’ve been using this approach in my recent work and it feels nice and works well.
CNET javascript update (r90)
May 16th, 2007 by Aaron N.I released a lot of code today including a bug fix that was probably pestering any of you with r87.
- product.picker.js now has no picklets; these are in the implementations/picklets directory
- ProductPicker now detects if there is no doctyp and, if not, sets the position of the picker to be fixed (no IE6 support)
- small docs update in element.cnet.js
- added new picklet: CNETProductPicker_PricePath
- added new picklet: NewsStoryPicker_Path
- new file: clipboard.js (allows you to insert text into the OS clipboard)
- new file: html.table.js (automates building html tables)
- new file: element.forms.js (for managing text inputs – get selected text information, insert content around selection, etc.)
- fixed an error in stickyWinHTML (ie reserves “class” for member names)
- converted window.onDomReady references to window.addEvent(‘domready’..
- updated css for stickyWin.js to avoid namespace conflicts with the css class “clearfix”
Sorting javascript arrays
May 9th, 2007 by Aaron N.Bas Wenneker has a nice little article over at solutoire.com on sorting javascript arrays. The docs over at Mozilla on Array.sort demonstrate the same stuff, but Bas’s article is a quick read that’s worth it if you ever see yourself working with data this way.
objectArray.sort(callbackFunc);
/**
* After sorting the objectArray will be like this:
* [
* {firstname: 'Will', lastname: 'Brown', age: 28},
* {firstname: 'John', lastname: 'Doe', age: 25},
* {firstname: 'Marie', lastname: 'Doe', age: 28},
* {firstname: 'Sarah', lastname: 'Doe', age: 25},
* {firstname: 'James', lastname: 'White', age: 28},
* {firstname: 'George', lastname: 'Williams', age: 25}
* ]
*/
new: Element.pin
May 3rd, 2007 by Aaron N.Here’s a little Mootools extension that you might find useful. I’m using it my StickyWin classes to allow the user to “pin” it in place so it won’t move if they scroll.
$('fxTarget').pin()
Execute the example, then scroll. That’s it. You can unpin it if you like:
$('fxTarget').unpin()
(note, in this example because of the way my little fxTarget helper works, unpin will break the drag behavior, but that won’t happen in other instances).
I’ve added .pin and .unpin to StickyWin, too.
Cookie.Json.js (a Mootools version of CookieJar)
April 16th, 2007 by Aaron N.UPDATE: This code is now in the plugins directory of the mootools library as Hash.Cookie.
Over on Ajaxian there was a recent post about a little Prototype.js dependent class called CookieJar.
Lalit Patel has created a JavaScript Library to use JSON to store data in cookies. JSON Cookies is built on top of Prototype and gives you a simple API to put and get JSON values into cookies
I liked the idea, so I wrote a version for Mootools. The Mootools version is a little different and adds some functionality (for merging data) and it stores its info a little differently, but it’s basically the same concept. Read the rest of this entry »
stickyWinHTML buttons: as many as you want
April 4th, 2007 by Aaron N.I’ve refactored stickyWinHTML (see this previous post) a bit more to allow for as many buttons as you like.
Here’s a quick code example:
var simpleLayoutExample = stickyWinHTML('the caption', 'this is the body', {
width: '400px',
buttons: [
{
text: 'close',
onClick: function(){alert('closed!')}
},
{
text: 'okey-dokey',
onClick: function(){alert('ok!')}
}
]
});
$('layoutExample').adopt(simpleLayoutExample);
The buttons array allows for as many buttons as you like. You can see more detail in the wikitorial on this topic.
Note: the old options (closeTxt, onClose, confirmTxt, and onConfirm) are still supported, though deprecated.
stickyWinHTML layout update: big close/ok buttons
March 13th, 2007 by Aaron N.I added large close and confirm buttons to the default stickyWinHTML layout. You can make them read whatever you want and you can assign callback functions to them getting clicked. They are optional, and not visible by default. Note the section in the link above about how the onClose event is NOT the same as the onClose event for StickyWin.
That little “x” for closing the StickyWin is pretty small, and on pages like in our CMS, I think an easier to click close button would make it easier to deal with these things, especially if you end up opening them often. If you don’t specify a closeTxt or confirmTxt value, the buttons are not there. The callbacks are not required. Example:
new StickyWin({
content: stickyWinHTML('the caption', 'this is the body', {
width: '400px',
closeTxt: 'close',
confirmTxt: 'okey-dokey',
onClose: function(){alert('closed!')},
onConfirm: function(){alert('confirmed')}
})});
Confirmer widget
March 8th, 2007 by Aaron N.Helping out our CMS group today I hacked out a new little widget class called “Confirmer” that handles notifying the user when something they’ve done on the page requires some sort of notice. An auto-save, or the failure to auto-save, or any other sort of message.
It’s designed to be unobtrusive so it doesn’t annoy the user too much if it happens a lot. You can have the prompt inline in the page or floating over it. The message just fades in and then back out. It comes with default styles that can easily be overridden, the message displayed can be changed every time you call it, and its easy to position it relative to an element or at the edge of the screen.
Quick example:
new Confirmer().prompt(); //watch the upper right of your screen
miniajax.com – a nice collection of patterns and scripts
March 1st, 2007 by Aaron N.Over at Ajaxian today they have a post on miniajax.com which describes itself as “a showroom of nice looking simple downloadable DHTML and Ajax scripts”.
I’m not sure if they authored these scripts or if they just collected them all into slick looking examples. Some of the items link to other sites, but some they seem to host.
Some of these things I think would do well to write as plugins for the Mootools library but if Valerio (the gatekeeper for Mootools) doesn’t want it I think some of them would do well to be added to the CNET Library.
This goes hand-in-hand with the patterns project that we’re trying to kick off here. I think this page could really help inform that work…