I came to the following solution: 

ScriptLoader async. loads scripts, executes deferred functions per script and 
provides a central synchronization point.

Comments welcome.

(To get the context, please follow the thread. I can comment my code if there's 
demand.)

It can be used like this:

/* ************************************************************************

#asset(peter/desktop/*)
#asset(peter/lib/*)

************************************************************************ */

qx.Class.define("peter.desktop.Application",
{
        extend : qx.application.Standalone,

        members :
        {
                main : function() {
                        this.base(arguments);

                        var loader = peter.lib.ScriptLoader.getInstance();
                        loader.addScript(peter.lib.D3.getInstance());

                        var t = this;
                        loader.load(function() {
                                console.log("ScriptLoader finished");
                                t.__main();
                        });

                        console.log("waiting on ScriptLoader");
                },              

                __main : function()
                {
                        var dayview = new peter.lib.DayView(new Date);
                        console.log(dayview.when());

                        var weekview = new peter.lib.WeekView(new Date);
                        console.log(weekview.when());
                }
        }
});



Library:



qx.Class.define("peter.lib.ScriptLoader", {
        type : "singleton",
        extend : qx.core.Object,
        members : {
                __scripts : {},
                __scriptCount : 0,
                loadScript : function(uri,onLoad) {
                        var req = new qx.bom.request.Script();
                        req.onload = onLoad;
                        uri = qx.util.ResourceManager.getInstance().toUri(uri);
                        console.log("GET " + uri);
                        req.open("GET", uri);
                        req.send();
                },
                addScript : function(script) {
                        if ( this.__scripts[script.label] ) {
                                return;
                        } 
                        this.__scripts[script.label] = script;
                        this.__scriptCount++;
                },
                load : function(onLoad) {
                        var finishedParts = 0;
                        var t = this;
                        var onFinishedPart = function () {
                                if ( ++finishedParts === t.__scriptCount ) {
                                        onLoad();
                                }
                        };
                        for ( var k in this.__scripts ) {
                                var script = this.__scripts[k];
                                console.log("loading script " + script.label);
                                this.loadScript(script.uri, function() {
                                        console.log("script " + script.label + 
" loaded");
                                        script.isLoaded = true;
                                        script.executeDefers();
                                        onFinishedPart();
                                });
                        }
                }
        }
});


qx.Class.define("peter.lib.Script", {
        type : "abstract",
        extend : qx.core.Object,
        members : {
                __defers : [],
                isLoaded : false,
                addDefer : function(defer) {
                        this.__defers.push(defer);
                },
                executeDefers : function() {
                        this.__defers.forEach(function(d) {
                                d();
                        });
                }
        }
});


qx.Class.define("peter.lib.D3", {
        extend : peter.lib.Script,
        type : "singleton",
        members :       {
                label : "d3",
                uri : "peter/lib/d3.v2.min.js"
        }
});


qx.Class.define("peter.lib.View", {
        extend : qx.core.Object,
        type : "abstract",

        construct : function(when) {
                this.base(arguments);
                this.__when = this.constructor.period(when);
        },

        members : {
                when : function() {
                        return this.__when;
                }
        }
});



/* ************************************************************************

#ignore(d3.time)

************************************************************************ */

qx.Class.define("peter.lib.DayView", {
        extend : peter.lib.View,

        statics : {
                period : null
        },

        defer : function(statics) {
                console.log("DayView defer");
                peter.lib.D3.getInstance().addDefer(function(){
                        console.log("DayView defer execution");

                        statics.period = d3.time.day;
                });
        }
});


/* ************************************************************************

#ignore(d3.time)

************************************************************************ */

qx.Class.define("peter.lib.WeekView", {
        extend : peter.lib.View,

        statics :       {
                period : null
        },

        defer : function(statics) {
                console.log("WeekView defer");
                peter.lib.D3.getInstance().addDefer(function(){
                        console.log("WeekView defer execution");

                        statics.period = d3.time.week;
                });
        }
});










------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
qooxdoo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to