From af68030fcf0c266300feb2c100149ecadef7d364 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sun, 16 Jul 2006 01:50:23 +0000 Subject: Merge from 3.0 branch till 1264. --- .../protected/pages/Advanced/Scripts1.page | 156 ++++++++++----------- 1 file changed, 78 insertions(+), 78 deletions(-) (limited to 'demos/quickstart/protected/pages/Advanced/Scripts1.page') diff --git a/demos/quickstart/protected/pages/Advanced/Scripts1.page b/demos/quickstart/protected/pages/Advanced/Scripts1.page index f11a2f9d..ef0b6317 100644 --- a/demos/quickstart/protected/pages/Advanced/Scripts1.page +++ b/demos/quickstart/protected/pages/Advanced/Scripts1.page @@ -6,8 +6,8 @@ Developer Notes for prototype.js by Sergio Pereira.

What is that?

-In case you haven't already used it, prototype.js is a - JavaScript library written by Sam Stephenson. +In case you haven't already used it, prototype.js is a + JavaScript library written by Sam Stephenson. This amazingly well thought and well written piece of standards-compliant code takes a lot of the burden associated with creating rich, highly interactive web pages that characterize the Web 2.0 off your back.

@@ -20,7 +20,7 @@ In case you haven't already used it, protot

As you read the examples and the reference, developers familiar with the Ruby - programming language will notice an intentional similarity between Ruby's + programming language will notice an intentional similarity between Ruby's built-in classes and many of the extensions implemented by this library.

@@ -32,7 +32,7 @@ In case you haven't already used it,
protot

- Unlike the DOM function, though, this one goes further. You can pass more than one id and + Unlike the DOM function, though, this one goes further. You can pass more than one id and $() will return an Array object with all the requested elements. The example below should illustrate this.

@@ -76,7 +76,7 @@ function test2()

Using the $F() function

- The $F() function is a another welcome shortcut. It returns the value of any field input control, + The $F() function is a another welcome shortcut. It returns the value of any field input control, like text boxes or drop-down lists. The function can take as argument either the element id or the element object itself.

@@ -100,9 +100,9 @@ function test3() into an Array object.

- This function, combined with the extensions for the Array class, - makes it easier to convert or copy any enumerable list into an - Array object. One suggested use is to convert DOM + This function, combined with the extensions for the Array class, + makes it easier to convert or copy any enumerable list into an + Array object. One suggested use is to convert DOM NodeLists into regular arrays, which can be traversed more efficiently. See example below.

@@ -113,8 +113,8 @@ function test3() - - + +

- This <a href="http://othersite.com/page.html">text</a> has - a <a href="#localAnchor">lot</a> of - <a href="#otherAnchor">links</a>. Some are + This <a href="http://othersite.com/page.html">text</a> has + a <a href="#localAnchor">lot</a> of + <a href="#otherAnchor">links</a>. Some are <a href="http://wherever.com/page.html">external</a> and some are <a href="#someAnchor">local</a>

-

@@ -309,21 +309,21 @@ function showLocalLinks(paragraph)

Enumerable Functions

The sample data for the following examples. -var Fixtures = +var Fixtures = { - Products: + Products: [ {name: 'Basecamp', company: '37signals', type: 'Project Management'}, {name: 'Shopify', company: 'JadedPixel', type: 'E-Commerce'}, {name: 'Mint', company: 'Shaun Inman',type: 'Statistics'} ], - Artist: + Artist: [ - 'As I Lay Dying', - '36 Crazyfist', - 'Shadows Fall', - 'Trivium', + 'As I Lay Dying', + '36 Crazyfist', + 'Shadows Fall', + 'Trivium', 'In Flames' ], @@ -334,11 +334,11 @@ var F = Fixtures;

Enumerable.each function

-

I used to find myself writing a lot of for loops. Although, -Prototype doesn’t by any means eliminate the need to do for loops, +

I used to find myself writing a lot of for loops. Although, +Prototype does not by any means eliminate the need to do for-loops, it does give you access to what I consider to be a cleaner, easier to read method in each. -for(var i = 0; i < F.Numbers.length; i++) +for(var i = 0; i < F.Numbers.length; i++) { Logger.info(F.Numbers[i]); } @@ -347,7 +347,7 @@ for(var i = 0; i < F.Numbers.length; i++) The each function allows us to iterate over these objects Ruby style.

-F.Numbers.each(function(num) +F.Numbers.each(function(num) { Logger.info(num); }); @@ -363,14 +363,14 @@ F.Numbers.each(function(num) 9 -

The each function takes one argument, an iterator function. -This iterator is invoked once for every item in the array, and that item -along with the optional index is passed to the iterator. So if +

The each function takes one argument, an iterator function. +This iterator is invoked once for every item in the array, and that item +along with the optional index is passed to the iterator. So if we also needed the index we could do something like the code below.

-F.Numbers.each(function(num, index) +F.Numbers.each(function(num, index) { Logger.info(index + ": " + num); }); @@ -387,11 +387,11 @@ F.Numbers.each(function(num, index)

Hash key/value pairs

-

Hashes can be created by wrapping an Object (associative array) in +

Hashes can be created by wrapping an Object (associative array) in $H() and can have their key/value pairs exposed.

-$H(F.Products[0]).each(function(product) +$H(F.Products[0]).each(function(product) { Logger.info(product.key + ": " + product.value); }); @@ -406,19 +406,19 @@ We can also directly access the keys and values of a Hash without iterating over

$H(F.Products[1]).keys(); -//Outputs name,company,type +//Outputs name,company,type $H(F.Products[1]).values(); -//Outputs Shopify,JadedPixel,E-Commerce +//Outputs Shopify,JadedPixel,E-Commerce

Enumerable.collect function

-

The collect function allows you to iterate over an Array and return the -results as a new array. Each item returned as a result of the iteration will be +

The collect function allows you to iterate over an Array and return the +results as a new array. Each item returned as a result of the iteration will be pushed onto the end of the new array.

-var companies = F.Products.collect(function(product) +var companies = F.Products.collect(function(product) { return product.company; }); @@ -431,7 +431,7 @@ Logger.info(companies.join(', '));

You can even join on the end of the block.

-return F.Products.collect(function(product) +return F.Products.collect(function(product) { return product.company; }).join(', '); @@ -439,9 +439,9 @@ return F.Products.collect(function(product)

Enumerable.include function

-

The include function allows you to check if a value is included in an array -and returns true or false depending on if a match was made. Assuming I put -up a form asking the user to name some artist in my iTunes playlist, +

The include function allows you to check if a value is included in an array +and returns true or false depending on if a match was made. Assuming I put +up a form asking the user to name some artist in my iTunes playlist, we could do something like the code below. Prime candidate for some conditional madness.

@@ -450,11 +450,11 @@ return F.Artists.include('Britney Spears'); // returns false

Enumerable.inject function

-

The inject function is good for getting a collective sum from an array of +

The inject function is good for getting a collective sum from an array of values. For instance, to add up all the numbers.

-var score = F.Numbers.inject(0, function(sum, value) +var score = F.Numbers.inject(0, function(sum, value) { return sum + value; }); @@ -463,19 +463,19 @@ Logger.info(score); //Output 161 -

The first argument to inject is just an initial value that +

The first argument to inject is just an initial value that would be added to the sum, so if we added 1 instead of 0, the output would be 162.

Enumerable.findAll function

-When given an Array, the findAll function will return an array of -items for which the iterator evaluated to true. Basically, it allows you to -build a new array of values based on some search criteria. -If we wanted to find all products whose type was “E-Commerce” +When given an Array, the findAll function will return an array of +items for which the iterator evaluated to true. Basically, it allows you to +build a new array of values based on some search criteria. +If we wanted to find all products whose type was “E-Commerce” we could do something like the code below.

-var ecom = F.Products.findAll(function(product) +var ecom = F.Products.findAll(function(product) { return product.type == 'E-Commerce'; }); @@ -486,18 +486,18 @@ Logger.info(ecom[0].company + " produces " + ecom[0].name); JadedPixel produces Shopify -

Note that even if only one match is made, just as in this case, -the result is still returned as an array. In that case, +

Note that even if only one match is made, just as in this case, +the result is still returned as an array. In that case, ecom.company would return undefined.

Enumerable.detect function

-

Unlike the findAll function, the detect function will only -return the first item for which the expression inside -the iterator is true. So, if we wanted to find the first number that +

Unlike the findAll function, the detect function will only +return the first item for which the expression inside +the iterator is true. So, if we wanted to find the first number that was greater than 5 we’d do something like the code below.

-var low = F.Numbers.detect(function(num) +var low = F.Numbers.detect(function(num) { return num > 5 }); @@ -506,13 +506,13 @@ Logger.info(low); //Outputs 98 -

Even though, there are other numbers above 5 in our array, detect +

Even though, there are other numbers above 5 in our array, detect only gives us the first match back.

Enumerable.invoke function

-

The invoke function allows us to pass a method as a string and -have that method invoked. For instance, if we wanted to sort +

The invoke function allows us to pass a method as a string and +have that method invoked. For instance, if we wanted to sort our array of artists we’d do something like this:

@@ -520,7 +520,7 @@ our array of artists we’d do something like this:

//Outputs 36 Crazyfist,As I Lay Dying,In Flames,Shadows Fall,Trivium
-

Why not just use F.Artists.sort? Well, for the example above +

Why not just use F.Artists.sort? Well, for the example above we could do just that, but here is where invoke shines.

@@ -533,8 +533,8 @@ we could do just that, but here is where invoke shines.

F.Artists.invoke('sort');
-

The reason this will not work is because it is taking each item -in that array and trying to apply sort to it, thus if we wrote it outright, +

The reason this will not work is because it is taking each item +in that array and trying to apply sort to it, thus if we wrote it outright, it would look something like this:

@@ -548,8 +548,8 @@ F.Artists.invoke('toLowerCase');

-Now, what about passing arguments to the invoke function? -The first argument passed to invoke is the method to be invoked, +Now, what about passing arguments to the invoke function? +The first argument passed to invoke is the method to be invoked, and any other arguments beyond that will be passed as arguments to the invoked method.

-- cgit v1.2.3