linkdata.each( function(conv){
li = document.createElement('li');
li.innerHTML = templ.evaluate(conv);
ul.appendChild(li);
});
The example starts with an empty unordered list retrieved using the Prototype dollar
function $(). After that, some link information, which is a normal array of object literals, is
declared. Next, a new string template, which is another feature of Prototype, is declared. The
link data goes through each one using the Prototype each() method. Prototype automatically
makes the each() method available on all arrays. Each item in the array is evaluated
into the template and spit into a new list item, which gets appended to the list. In this case,
the link data is embedded in the script. A more common scenario is to pull in the link data
via an Ajax call.
Working with Collections
A collection is an array of objects, and the array functionality within JavaScript can be limiting.
Prototype, for example, includes a very robust Enumerable class for working with collections.
You can use methods that will automatically scan the array and remove elements, add elements,
or return only a subset of elements.
As you saw in the last example, the each() method on the array was used to loop through
the array. Iteration is much simpler than having to create for loops every time.
Handling JSON and XML
The need to handle data sets is tied mostly to Ajax. These data sets need to be transferred in
a format that enables you to quickly understand how the data is structured.
Pages:
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145