XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

What's New in Prototype 1.5?
by Scott Raymond | Pages: 1, 2, 3, 4, 5

  • match(selector) takes a single CSS selector expression (or Selector instance) and returns true if it matches the element. For example:

    $('target').match('div'); // => true

  • readAttribute(name) returns the value of the element's attribute named name. Useful in conjunction with Enumerable.invoke for extracting the values of a custom attribute from a collection of elements. For example:

    // <div id="widgets">
    //   <div class="widget" widget_id="7">...</div>
    //   <div class="widget" widget_id="8">...</div>
    //   <div class="widget" widget_id="9">...</div>
    // </div>

    $$('div.widget').invoke('readAttribute', 'widget_id') // ["7", "8", "9"]

  • update(html) replaces the innerHTML property of the element with html. If html contains <script> blocks, they will not be included, but they will be evaluated. As of version 1.5, update() works with table-related elements and can take a nonstring parameter, or none at all. For example:

    $('target').update('Hello');
    $('target').update() // clears the element
    $('target').update(123) // set element content to '123'

  • hasAttribute(attribute) returns whether the element has an attribute named attribute. Despite being a standard DOM method, not all browsers implement this method, so Prototype spans the gap. For example:

    $('target').hasAttribute('href');

  • While the members of Element.Methods are added to every element, form-related elements also get the methods defined in Form.Methods and Form.Element.Methods. Two new such methods were added to Form.Element.Methods in Prototype 1.5: enable() and disable(), which make the element editable or locked. For example:

    $('target').enable();
    $('target').disable();

  • Backward compatibility change: toggle(), show(), and hide(), as well as Field.clear() and Field.present(), no longer take an arbitrary number of arguments. Before upgrading to Prototype 1.5, check your code for instances like this:

    Element.toggle('page', 'sidebar', 'content') // Old way; won't work in 1.5
    ['page', 'sidebar', 'content'].each(Element.toggle) // New way; 1.5-compatible

    Element.show('page', 'sidebar', 'content') // Old way; won't work in 1.5
    ['page', 'sidebar', 'content'].each(Element.show) // New way; 1.5-compatible

    Element.hide('page', 'sidebar', 'content') // Old way; won't work in 1.5
    ['page', 'sidebar', 'content'].each(Element.hide) // New way; 1.5-compatible

...And More!

  • Object.clone(object) returns a shallow clone of object, such that the properties of object that are themselves objects are not cloned. For example:

    original = {name: "Sam", age: "21", car:{make: "Honda"}};
    copy = Object.clone(original);
    copy.name = "Marcel";
    copy.car.make = "Toyota";
    original.name; // "Sam"
    original.car.make; // "Toyota"

  • Object.keys(object) returns an array of the names of object's properties and methods. For example:

    Object.keys({foo:'bar'}); // => ["foo"]

  • Object.values(object) returns an array of the values of object's properties and methods. For example:

    Object.values({foo:'bar'}); // => ["bar"]

  • Instances of PeriodicalExecutor have a new method, stop(), which will stop performing the periodical tasks. After stopping, the object will call the callback given in the onComplete option, if any.

  • Function.prototype.bindAsEventListener() now takes an arbitrary number of arguments. Any additional arguments after the first (an object) will be passed through as arguments to the function.

  • The Prototype developers maintain a suite of unit tests alongside the library itself, verifying that the code works—and keeps working—across a range of browsers. The test coverage in this release has skyrocketed, with an incredible 20-fold increase in the number of assertions. The testing infrastructure has matured remarkably as well: with one shell command (rake test, which requires Ruby and the Rake library), Prototype's tests are automatically run in every browser found on your system, and the results are displayed. You take advantage of the same infrastructure to test your own JavaScript, thanks to unittest.js, part of the script.aculo.us library. The status of the JavaScript test run can even be automatically integrated with the other unit tests in your system. If you are building a JavaScript-heavy web application, that safety net can be a life-saver (or more likely, a job-saver.)

  • In addition to all of the new features described above, this release includes 68 bug fixes, cross-platform compatibility enhancements and performance improvements. And over 60 contributors were acknowledged—all of whom deserve credit for making this release such a remarkable one.

In this article, we've looked at all of the significant additions in Prototype 1.5. It's a wealth of new features, but it scarcely compares to the full breadth of Prototype's functionality. With just a few exceptions, all of the method descriptions and examples in this guide were drawn from my Prototype Quick Reference, which covers the entire library in just as much detail. The same content is available in print as part of my book, Ajax on Rails.



1 to 1 of 1
  1. Incorrect parameter encoding?
    2007-01-28 20:13:14 ickmonst
1 to 1 of 1