Prototype 1.5 new features (translation notes)

Source: Internet
Author: User
Original Author: Scott Raymond
Translation notes: Rubyoo

Original: http://www.xml.com/pub/a/2007/01/24/whats-new-in-prototype-15.html
Source: http://www.rubyoo.com/

With the world's greatest web development Framework, the official release of Rails 1.2, the world's most popular JavaScript development framework
Prototype 1.5 was also released.

Prototype 1.4 has been a year since it was released, with a number of exciting new features compared to 1.4,prototype 1.5.

Support for Ajax
Prototype 1.5 adds powerful performance features on the basis of prototype 1.4, as follows:

1. Query parameters can be expressed in object literal quantities, for example:
View Plain | Print | Copy to Clipboard |?
1 Requests/search?q=ajax%20tutorials
2 New Ajax.request ('/search ', {parameters:{Q: ' Ajax tutorials '}});
Requests/search?q=ajax%20tutorials new Ajax.request ('/search ', {parameters:{Q: ' Ajax Tutorials '}});

2. Similar to query parameters, the sending request can also be passed directly through the object literal, for example:
View Plain | Print | Copy to Clipboard |?
1 New Ajax.request ('/search ', {requestheaders:{x-custom-header: ' value1 '}});
New Ajax.request ('/search ', {requestheaders:{x-custom-header: ' value1 '}});

3. The sending request part may use accept, the default value is (Text/javascript, text/html, Application/xml, Text/xml, */*). For example:
View Plain | Print | Copy to Clipboard |?
1 New Ajax.request ('/data ', {requestheaders:{Accept: ' Text/plain '}});
New Ajax.request ('/data ', {requestheaders:{Accept: ' Text/plain '}});

The 4.contentType option is used to specify ContentType, and the default value is application/x-www-form-urlencoded;encoding to specify the encoding, which defaults to UTF-8. For example:
View Plain | Print | Copy to Clipboard |?
1 var myxml = "<?xml version= ' 1.0 ' encoding= ' utf-8 '" >/n<rss version= ' 2.0 ' >...</rss> '
2 New Ajax.request ('/feeds ', {postbody:myxml, ContentType: ' Application/rss+xml ', Encoding: ' UTF-8 '});
var myxml = "<?xml version= ' 1.0 ' encoding= ' utf-8 '"? >/n<rss version= ' 2.0 ' >...</rss> ' new Ajax.request ( '/feeds ', {postbody:myxml, ContentType: ' Application/rss+xml ', Encoding: ' UTF-8 '});

5. If you are communicating with the rails1.2 restful, you can specify two methods other than Get and post: the Put and DELETE. For example:
View Plain | Print | Copy to Clipboard |?
1 Creates a POST request To/feeds/1.rss?_method=put
2 New Ajax.request ('/feeds/1.rss ', {method: ' Put ', Postbody:myxml, ContentType: ' Application/rss+xml '});
Creates a POST request To/feeds/1.rss?_method=put new Ajax.request ('/feeds/1.rss ', {method: ' Put ', postbody:myxml, con Tenttype: ' Application/rss+xml '}); Of course, this requires server support. If you are using rails1.2, then this can be very easy.



Extensions to Strings

Prototype 1.5 is an extension of the string, making it very convenient and friendly to manipulate the string.

1.strip () Remove the newline characters at both ends of the string, white space, and so on, for example:
View Plain | Print | Copy to Clipboard |?
1 "foo". Strip (); => "Foo"
"foo". Strip (); => "Foo"

2.gsub (pattern, replacement) performs a global substitution of strings, for example:
  View Plain | print | Copy to Clipboard |?
 
1 "In all things will i obey". Gsub ("All",  "all"); // =>  "In all things will i obey"  
2 "In all things will i obey". Gsub (/[aeiou]/i,  "_");  // =>  "_n _ll th_ngs w_ll _ _b_y"  
3 In all things will i obey. Gsub (/[aeiou]/i, function (x) { return x [0].touppercase ()  }); // =>  "In all things will i obey"  
4 ' Sam stephenson '. Gsub (/(/w+)   (/w+)/,  ' #{2}, #{1} ');  //& nbsp;=>  "Stephenson, sam" &NBSP;
"In all things would I obey". Gsub ("All", "all"); => "In all things would I obey" "in all things would I obey". Gsub (/[aeiou]/i, "_"); => "_n _ll th_ngs w_ll _ _b_y" "In all things would I obey". Gsub (/[aeiou]/i, function (x) {return x[0].touppercase (); }); => "In all things I ObEy" ' Sam Stephenson '. Gsub (/(/w+) (/w+)/, ' #{2}, #{1} '); => "Stephenson, Sam."

3.sub (Pattern, replacement[, Count]) is similar to the Gsub function, except that it is not a global replacement.
  View Plain | print | Copy to Clipboard |?
 
1 In all things&nbs P;will i obey ". Sub (/[aeiou]/i, " _ "); // => " _n all things will  i obey " 
2 " In all things will i obey ". Gsub (/[aeiou]/i, " _ ",  3); // => " _n _ll th_ngs will i  Obey " 
3 ' Sam stephenson '. Sub (/(/w+)   (/w+)/,  ' #{2}, # {1} '); // =>  "Stephenson, sam" &NBSP;
"In all things I obey". Sub (/[aeiou]/i, "_"); => "_n All things would I obey" "in all things would I obey". Gsub (/[aeiou]/i, "_", 3); => "_n _ll th_ngs'll I Obey" ' Sam Stephenson '. Sub (/(/w+) (/w+)/, ' #{2}, #{1} '); => "Stephenson, Sam."

4.scan (pattern, iterator) is scanned in patterns mode and each matching part is treated with iterator, for example:
View Plain | Print | Copy to Clipboard |?
1 Logs each vowel to the console
2 "Prototype". Scan (/[aeiou]/, function (match) {Console.log (match);})
Logs each vowel to the console "Prototype". Scan (/[aeiou]/, function (match) {Console.log (match);})

5.truncate ([length[, truncation]]) intercepts a partial string with the default length of 30. For example:

  View Plain | print | Copy to Clipboard |?
 
1 FOUR&NBSP;SCORE&NBSP;AND&NB Sp;seven years ago our fathers brought ". Truncate (); // => " Four  score and seven years  ... " 
2 " four score& Nbsp;and seven years ago our fathers brought ". Truncate (; // =>)   "Four score and se ..."  
3 four score and& Nbsp;seven years ago our fathers brought ". Truncate (30,  '   (read more) ') ; // =>  "four score and sev  (Read more)" &NBSP;
"Four score and seven years ago our fathers brought". Truncate (); => "Four score and seven years ..." "Four score and seven years ago our fathers brought". Truncate (20); => "Four score and Se ..." "Four score and seven years ago we fathers brought". Truncate (read more); => "Four score and SEv (read more)"


6.capitalize () to implement the first letter uppercase. For example:
View Plain | Print | Copy to Clipboard |?
1 "Prototype". Capitalize (); => "Prototype"
"Prototype". Capitalize (); => "Prototype"

7.dasherize () turns the underline into an underscore, for example:
View Plain | Print | Copy to Clipboard |?
1 "Hello_world". Dasherize (); => "Hello-world"
2 "Hello_world". Dasherize (); => "Hello-world"
"Hello_world". Dasherize (); => "Hello-world" "Hello_world". Dasherize (); => "Hello-world"

8.underscore () "::" Into "/", the camel representation into the expression of underline, the middle line into the underline, and finally converted to lowercase.
For example:
View Plain | Print | Copy to Clipboard |?
1 "Foo::bar". underscore (); => "Foo/bar"
2 "borderBottomWidth". underscore (); => "Border_bottom_width"
"Foo::bar". underscore (); => "Foo/bar" "borderBottomWidth". underscore (); => "Border_bottom_width"

9.SUCC () returns the next character. For example:
View Plain | Print | Copy to Clipboard |?
1 "ABCD". SUCC (); => "ABCE"
2 $R (' A ', ' d '). Map (function (char) {return char;}); => [' A ', ' B ', ' C ', ' d ']
"ABCD". SUCC (); => "ABCE" $R (' a ', ' d '). Map (function (char) {return char;}); => [' A ', ' B ', ' C ', ' d ']

Prototype 1.5 adds a class called Template (Template), using the following methods:

View Plain | Print | Copy to Clipboard |?
1 var row = new Template (' <tr><td>#{name}</td><td>#{age}</td></tr> ');
var row = new Template (' <tr><td>#{name}</td><td>#{age}</td></tr> ');

Execute with the Evaluate method, for example:
View Plain | Print | Copy to Clipboard |?
1 var person = {name: ' Sam ', age:21};
2 Row.evaluate (person); => ' <tr><td>Sam</td><td>21</td></tr> '
3 Row.evaluate ({})); => ' <tr><td></td><td></td></tr> '
var person = {name: ' Sam ', age:21}; Row.evaluate (person); => ' <tr><td>Sam</td><td>21</td></tr> ' Row.evaluate ({})); => ' <tr><td></td><td></td></tr> '

The default template syntax is Ruby-style, such as #{age}, and if you want to use a different template syntax style, you can reassign it, for example:
Using the syntax of PHP

View Plain | Print | Copy to Clipboard |?
1 Template.phppattern =/(^|.| /r|/n) (</?=/s*/$ (. *?) /s*/?>)/;
2 var row = new Template (' <tr><td><?= $name? ></td><td><?= $age? ></td></tr > ', Template.phppattern);
3 Row.evaluate ({name: ' Sam ', age:21}); "<tr><td>Sam</td><td>21</td></tr>"
Template.phppattern =/(^|.| /r|/n) (</?=/s*/$ (. *?) /s*/?>)/; var row =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.