Include and Promise for Prototype





6
Date Submitted Fri. Sep. 28th, 2007 2:08 AM
Revision 1 of 1
Scripter Fordiman
Tags JavaScript | promise | Prototype
Comments 6 comments
Here's a quick lil' addon for Prototype that I use often.

Prototype.Promise(condition, action, interval)
condition is a string that you want met before an action is run.
action is a function that does the action.
interval is the polling rate for condition in seconds, and defaults to 1

So, for example, you may want function foo to run, but only once bar has been set:

function foo(a,b) {
this.retVal=a+b;
}
var thingy = {
retVal:0
};
Prototype.Promise(
'thingy.retVal=5',
foo.bind(thingy,5,10),
5
);


Then, in some point in the mysterious future, thingy.retVal gets set to 5, at which point, the Promise goes into effect, and thingy.retVal becomes 10.

Where I find this particularly useful is in making sure that a document is loaded before doing something (condition="$$('body').length>=1), as you can see it used for the include functions.

Speaking of which, the following include functions are great for getting scripts and stylesheets into your page. I won't bother with examples, as they're pretty straightforward.

Meanwhile, Prototype.scriptPath will point to wherever in your server's heirarchy Prototype was loaded from. The regex, you'll note allows for names like prototype.compressed.js, prototype.modified.js, 2007-09-28.prototype.js, etc - just in case you want to keep track of your various hacks of Prototype, as I do.

Object.extend(Prototype,
        {
                scriptPath:
                        $$('script').find(
                                        function (s) {
                                                return (s.src && /prototype.*js$/i.test(s.src))
                                        }
                                ).src.replace(/prototype.*js$/i,''),
                includeJS: function (file) {
                        var elScript = Element.create('script',{
                                type:'text/javascript',
                                src:file
                        });
                        var myPromise = (function () {$$('head')[0].appendChild(this);}).bind(elScript);
                        Prototype.Promise(
                                '$$("head").length>=1',
                                (function () {
                                        $$('head')[0].appendChild(this);
                                }).bind(elScript)
                        );
                },
                includeCSS: function (file) {
                        var elLink = Element.create('link',{
                                type:'text/css',
                                rel:'stylesheet',
                                href:file
                        });
                        Prototype.Promise(
                                '$$("head").length>=1',
                                (function () {
                                        $$('head')[0].appendChild(this);
                                }).bind(elLink)
                        );
                },
                Promise: function (condition,action,interval) {
                        if (typeof interval=='undefined') interval=1;
                        //Promise to do this now, or whenever (condition) is met
                        if (eval(condition))
                                action();
                        else
                                new PeriodicalExecuter(
                                        function (pe) {
                                                Prototype.Promise.bind(pe,condition,action,interval)();
                                        },
                                        2
                                );
                        if (this.stop) this.stop();
                }
        }
);
 

Bryan Elliott

Comments

Comments Bound functions
Fri. Sep. 28th, 2007 2:15 AM    Scripter Fordiman
Comments Figured it out
Fri. Sep. 28th, 2007 2:19 AM    Scripter Fordiman
Comments Alternative Promise
Thu. Dec. 20th, 2007 6:22 PM    Scripter Fordiman
Comments Onload
Mon. Oct. 22nd, 2007 7:23 PM    Helper mceppi
  Comments Additional
Fri. Dec. 14th, 2007 4:30 PM    Scripter Fordiman
  Comments Observe(winload) vs. Prom
Sat. Dec. 1st, 2007 2:05 PM    Scripter Fordiman

Voting