What's new in Prototype svn?
Just after posting this I saw that sam tagged the 1.5.1 release in svn so I guess this means that 1.5.1 is final. Great work!
There has been a lot of action the past days in the Prototype SVN; the release of 1.5.1 RC4, the new DOM and Position branches and a lot of other work. Since I watch most of the tickets and changesets I thought I write something about what’s (might be) in the future for Prototype.
DOM branch
I was happy to see a patch I’ve been working on (#7476) to get included some days ago.
It will define a new Element#writeAttribute
method which will work together as the already existing #readAttribute
to work around many issues browsers having with the native set/getAttribute
methods (as always mostly in IE) but also to make chaining work:
$('my_image').writeAttribute('src', 'new-image.png').show();
The method will also accept a hash for the attributes to let you set multiple attributes at once:
$('my_image').writeAttribute({src: 'new-image.png', custom_attr: 'foo'}).show();
The patch will also make the Element
object into a constructor for very easy and fast element creation (i.e. document.createElement
wrapper):
var header = new Element('h1');
The constructor will also accept an optional attributes hash for the attributes (as seen above in the #writeAttribute
method):
var header = new Element('h1', {id: 'myHeader'});
Of course chaining will work so for a simple image switcher with preloading you could do this:
<!-- html --><br />
<img src="thumbnail.jpg" id="thumbnail" hires="big.jpg">
// javascript<br />
$('thumbnail').observe('click', function(event) {<br />
var thumb = Event.element(event),<br />
preloader = new Element('img', {src: thumb.readAttribute('hires')});<br />
preloader.observe('load', function(event) {<br />
thumb.writeAttribute('src', preloader.readAttribute('src'));<br />
});<br />
});
Event Branch
I haven’t followed the event branch very closely but from the code and messages on the ML I’ve seen there should be new functionality for better garbage collection (to prevent memory leaks) and for e.g. easier removal of observers from elements.
Where you before had to manually collect all an element’s event listeners to be able to remove them later:
var listener1 = function(event) {...}.bindAsEventListener();<br />
var listener2 = function(event) {...}.bindAsEventListener();
// start listening<br />
$('myElement').observe('click', listener1);<br />
$('myElement').observe('click', listener2);
// stop listening<br />
$('myElement').stopObserving('click', listener1);<br />
$('myElement').stopObserving('click', listener2);
you will now be able to remove all event listener for a specfic event:
$('myElement').stopObserving('click'); // or Event.stopObserving('myElement', 'click');
Other
Some other new additions worth mentioning are:
But to not make this post too long you’ll have to read about it on your own (check the source and/or the changesets manually).
Please notice that all these are subject to change since it’s under development right now and might never get into any future release of Prototype.