Book Image

MooTools 1.3 Cookbook

By : Jay L Johnston
Book Image

MooTools 1.3 Cookbook

By: Jay L Johnston

Overview of this book

MooTools is a JavaScript framework that abstracts the JavaScript language. JavaScript itself, complex in syntax, provides the tools to write a layer of content interaction for each different browser. MooTools abstracts those individual, browser-specific layers to allow cross-browser scripting in an easy-to-read and easy-to-remember syntax. Animation and interaction, once the domain of Flash, are being taken by storm by the MooTools JavaScript framework, which can cause size, shape, color, and opacity to transition smoothly. Discover how to use AJAX to bring data to today's web page users who demand interactivity without clunky page refreshes. When searching for animation and interactivity solutions that work, MooTools 1.3 Cookbook has individual, reusable code examples that get you running fast! MooTools 1.3 Cookbook readies programmers to animate, perform AJAX, and attach event listeners in a simple format where each section provides a clear and cross-browser compatible sketch of how to solve a problem, whether reading from beginning to finish or browsing directly to a particular recipe solution. MooTools 1.3 Cookbook provides instant solutions to MooTools problems – whatever you want to do with MooTools, this book will tell you how to do it. MooTools 1.3 Cookbook is presented in a progressive order that builds concepts and ideas, while simultaneously being a collection of powerful individual, standalone, recipe solutions.
Table of Contents (17 chapters)
MooTools 1.3 Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Storing a list of names in an array of values


In this recipe we will learn how to use a standard data structure called an Array to store a list of names or values.

Getting ready

To create an array, a storage element that holds a list of values, elements, or objects, we use the raw JavaScript Array object to define a literal array var myarray = [1, 2, 'my 3rd value'];. In our example, we first declare our array; it is in an empty state, then we call upon either raw JavaScript's push() array object method or MooTools' extension of the array native include(), based on the ternary output, to add our string value to the array.

How to do it...

Add items to an array. Allow or disallow the addition of duplicate items with a switch in the form.

<script type="text/javascript" src="mootools-1.3.0.js"></script>
</head>
<body>
<form action="" method="get">
<input type="text" id="myitem"/>
Ignore if already in the array?
<input type="checkbox" id="unique"/>
<input type="button" id="mybutton"
value="Add This"
onclick="store_item_in_array();"/>
</form>
<script type="text/javascript">
// declare the array
var myarray = [];
// an array-dedicated utility function to add elements
function store_item_in_array() {
// use the $ object to get element with id "myitem"
var myitem = $('myitem').value;
// ternary operators can save a lot of space
var ischecked = $('unique').get('checked');
var unique = (ischecked) ? 1 : 0;
if (!unique) {
// (A) add an item to an array with raw JavaScript
myarray.push(myitem);
} else {
// (B) add an item to an array, but make it moonique
myarray.include(myitem);
}
alert('Length of Array: '+myarray.length);
}
</script>

How it works...

Much like $, arrays are infused with methods that extend their capability. This snippet calls the JavaScript inherent push(), which post-pends the single argument to the array, like this: var myarray.push('myvalue');.

Note

NOTE: The JavaScript array object will hold much more than string values: it can hold integers, objects, even other array objects to create multi-dimensional arrays!

MooTools has further extended the array prototype by adding new, useful methods. The method used here is called include(), which works identically both in syntax and in function to push(); however, it adds a duplicate value check to the incoming argument. If one or more values present are matched, the function does not add a value to the array.

There's more

We should open up the uncompressed version of our MooTools and search for the phrase "include". We can quickly see how include() is an abstraction that enhances push().