Adam Gotterer

Find the secrets to infinite income, and automate it!

Archive for the ‘Prototype XUL’ Category

Using Prototype XUL

The examples here are taken from Function page of the Prototype JS api documentation. They have been re-written to reflect the Prototype XUL changes. Hope they help!

argumentNames

var fn = function(foo, bar) {
   return foo + bar;
};
 
argumentNames(fn); //-> ['foo', 'bar']

bind

var fn = function(foo, bar) {
   return foo + bar;
};
 
argumentNames(fn); //-> ['foo', 'bar']

bindAsEventListener

var obj = { name: 'A nice demo' };
 
function handler(e) {
  var tag = Event.element(e).tagName.toLowerCase();
  var data = $A(arguments);
  data.shift();
  alert(this.name + '\nClick on a ' + tag + '\nOther args: ' + data.join(', '));
}
 
Event.observe(document, 'click', bindAsEventListener(obj, handler, 1, 2, 3));

curry

String.prototype.splitOnSpaces = curry(String.prototype.split, " ");
"foo bar baz thud".splitOnSpaces(); //-> ["foo", "bar", "baz", "thud"]

defer

function hideNewElement() {
  $('inserted').hide();
};
 
function insertThenHide(markup) {
  $('container').insert(markup);
 
  // Prototype XUL
  defer(hideNewElement);
}
 
insertThenHide("
<p id="inserted">Lorem ipsum
 
");

delay

// before:
window.setTimeout(function() {
Element.addClassName('foo', 'bar'); }, 1000);
 
// after:
delay(Element.addClassName, 1, 'foo', 'bar');
 
// clearing a timeout
var id = delay(Element.hide, 5, 'foo');
window.clearTimeout(id);

delay

// start off with a simple function that does an operation
// on the target object:
var fn = function(target, foo) {
  target.value = foo;
};
 
var object = {};
 
// use the original function
fn(object, 'bar');
object.value //-> 'bar'
 
// if we methodize it and copy over to the object, it becomes
// a method of the object and takes 1 argument less:
 
object.fnMethodized = methodize(fn);
 
object.fnMethodized('boom!');
object.value //-> 'boom!'

wrap

String.prototype.capitalize = wrap(String.prototype.capitalize, 
  function(proceed, eachWord) {
    if (eachWord && this.include(" ")) {
      // capitalize each word in the string
      return this.split(" ").invoke("capitalize").join(" ");
    } else {
      // proceed using the original function
      return proceed();
    }
  }); 
 
"hello world".capitalize()     // "Hello world"
"hello world".capitalize(true) // "Hello World"

Prototype XUL Documentation

For the most part the functionality of Prototype XUL works the same as normal prototype. The prototype documentation can be found here. The underlying changes are in the methods that extend Prototype.function. The functions that have been re-written are: argumentNames, bind, bindAsEventListener, curry, delay, defer, wrap and methodize. The core difference in use, is that these functions can not be extended from objects anymore and must be explicitly called.

argumentNames

argumentNames(someFunction) //-> Array

bind

bind(thisObj, someFunction) //-> Function

bindAsEventListener

bindAsEventListener(thisObj, someFunction) //-> Function

curry

curry(someFunction, [args...]) //-> Function

defer

curry(someFunction, [args...]) //-> Number

delay

delay(someFunction, seconds, [args...]) //-> Number

methodize

delay(someFunction, [args...]) //-> Function

wrap

delay(wrapperFunction, someFunction, [args...]) //-> Function

Prototype XUL Update

While writing out the Prototype XUL documention I came across a nasty little bug that took me a few hours to fix. I will wrap up the documentation tomorrow. If you downloaded the Prototype XUL 1.6.0.2 file anytime before today, please upgrade (the version number has not change).

Drum roll please… After three attempts/failures, I present to you the newest version of the Prototype XUL library! The previous released version technically never worked. It was something I threw together for a Firefox project that only required the basic of functionality (Ajax.Request, utility functions, etc.). I was under the impression that converting the functions that failed due to the known XUL function bug would solve all the problems. Unfortunately this was not the case and under these assumptions more complex functionality ceased to work correctly. This version has gone through a finer examination, conversion and code update to (hopefully) be a fully functional representation of prototype. Hopefully Firefox 3 will have this bug corrected and I wont have to release many future updates. Please post any bug reports, questions or feedback in the comments section, this will help reduce duplication and make my life easier :). Enjoy!

WARNING: I have not fully tested this library!

Disclaimer: This modified version of the Prototye JavaScript framework is in no way affiliated, distributed or supported by Sam Stephenson or the Prototype library project. This library has been modified from its original intended version/use and I take no responsibility for any bugs, damages or problems this library may cause.

Download Prototype XUL

* Most up to date version as of 2/22/08
* Documentation
* Examples

A few days ago I posted about a FireFox bug that affected Function.prototype in the Chrome environment. The two modified prototype libraries that I posted were starting to get a little outdated. I spent the night modifying the most recent version of Prototype 1.6.0_rc1 framework to work in Chrome. I have also removed as many references as I could find to browsers other then Mozilla/Gecko. If you find any bugs or make any changes, please let me know so I can update the posted code.

WARNING: I have not fully tested this library!

Disclaimer: This modified version of the Prototye JavaScript framework is in no way affiliated, distributed or supported by Sam Stephenson or the Prototype library project. This library has been modified from its original intended version/use and I take no responsibility for any bugs, damages or problems this library may cause.

Download Prototype XUL

One of the projects that I have been working on is a FireFox extension. I’m pretty new to the XUL/Extension space, but have a lot of JavaScript experience. My framework of choice has long standing been Prototype. I figured why not make life easy and utilize that functionality in the extension. I soon realized that almost none of the functionality worked in the Chrome environment. I spent a lot of time reinventing the wheel, re-writing many of the general prototype features. When I started writing my own bind function I realized that it was throwing erroneous errors. I know the code was perfect and successfully tested it in a front end environment. I started doing some research and found out theres a long standing bug in the FireFox Chrome environment that prevents extending Function.prototype. Some more research and I found that the reason most of the prototype functionality didn’t work was because the methods require use of the bind function. I found a hacked version of prototype 1.4 that has been re-written to work in Chrome and a bind patch for prototype 1.5.

Update: I modified the most recent version of Prototype (1.6.0 RC1) to work in Chrome. You can download the code here.