Also note that when using a bound function as the action, the this. keyword isn't actually valid. I'm trying to think up a way to get that working, but for now, no dice.
I was trolling around the Prototype Function API, and I thought, 'Hey, the Promise function would be better suited as part of Function's prototype...'
So, here it is.
Function.prototype.promise = function(/* conditional function, polling interval, [argument,...] */){ var __method=this,
oArgs=arguments,
args=$A(arguments),
__cond=args.shift(),
__intv=args.shift(); if(__cond.apply(__cond,args)) this.apply(this,args); elsenew PeriodicalExecuter(function(pe){
__method.promise.apply(__method,oArgs);
pe.stop(); },__intv); } /* Example of use */ function f(){ alert("I've kept my promise."); } function c(){ return f.value==4; }
f.value=5;
f.promise(c,1); /*Not necessarily how c()'s results will be changed in a real world
example, but a fine way to test it in this situation */
setTimeout("f.value--;", 10);
Not to trump your function, it's a great add-on. However to detect a page load in Prototype you could simply use the following from http://prototypejs.org/api/event/observe
function init() { //Do something }
Event.observe(window, 'load', init);
Concerning making a promise on the occurence of $$('body').length>0:
A page load is an event, and only occurs once. Attaching an event handler to window.load later in the game means your function will never run. That's why it's used that way in Include, as such behavior allows an include to be requested before or after the page loads, and it'll work without fail.
Heh. Ever do something nifty and forget *why* you did it in the first place?
I know observe, and use it constantly. I was just giving an example, and a bad one at that. Still, a lot of things don't have an event associated with them.
One thing I've used it for that's a good example is an auto-image uploader. I'll spare you the gories of the implementation, but the simple explanation is that the image upload button is a properly skinned iframe with an opacity=0 input[type=file]. I'd like to have it set up so that once an image is selected, it's uploaded and called back to the parent window with the image's location. Unfortunately, there's no event for this.
The solution: Promise to submit the input's form when its value is nonempty.
There are other uses. The point is that while observe and Promise do have some functionality overlap (as event handling and polling are certain to have at some point or another), Promise does fill a gap in the case of awkward programming situations in an elegant way (yeah, I know it's brute force polling, but when that's the only solution, it's not a bad thing for the frontal code to look elegant).
Promise: function (condition,action,interval) {
if (typeof interval=='undefined') interval=1;
if (condition())
action();
else
new PeriodicalExecuter(
function (pe) {
Prototype.Promise.bind(pe,condition,action,interval)();
},
2
);
if (this.stop) this.stop();
}
So, here it is.
var __method=this,
oArgs=arguments,
args=$A(arguments),
__cond=args.shift(),
__intv=args.shift();
if (__cond.apply(__cond,args))
this.apply(this,args);
else new PeriodicalExecuter(function (pe) {
__method.promise.apply(__method,oArgs);
pe.stop();
},__intv);
}
/* Example of use */
function f() {
alert("I've kept my promise.");
}
function c() {
return f.value==4;
}
f.value=5;
f.promise(c,1);
/*Not necessarily how c()'s results will be changed in a real world
example, but a fine way to test it in this situation */
setTimeout("f.value--;", 10);
{
//Do something
}
Event.observe(window, 'load', init);
A page load is an event, and only occurs once. Attaching an event handler to window.load later in the game means your function will never run. That's why it's used that way in Include, as such behavior allows an include to be requested before or after the page loads, and it'll work without fail.
Heh. Ever do something nifty and forget *why* you did it in the first place?
One thing I've used it for that's a good example is an auto-image uploader. I'll spare you the gories of the implementation, but the simple explanation is that the image upload button is a properly skinned iframe with an opacity=0 input[type=file]. I'd like to have it set up so that once an image is selected, it's uploaded and called back to the parent window with the image's location. Unfortunately, there's no event for this.
The solution: Promise to submit the input's form when its value is nonempty.
There are other uses. The point is that while observe and Promise do have some functionality overlap (as event handling and polling are certain to have at some point or another), Promise does fill a gap in the case of awkward programming situations in an elegant way (yeah, I know it's brute force polling, but when that's the only solution, it's not a bad thing for the frontal code to look elegant).