interesting. I created something similar to this for my server-side
mootools project a while back. But this is a little more sleek. I'll
have to give it a try.
On Oct 14, 8:28 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> Hope can be useful to anyone:
>
> I've created a little OOP extension to mootools for a project I'm
> working on:
>
> It's a Namespace function to define easily namespaces and a "require"
> function that allow a javascript file to require another file, being
> loaded on the fly.
>
> You have to define before using require a global variable called
> requiredPath to the path where to start loading javascripts.
>
> the syntax is:
> requiredPath="/path/to/scripts"
>
> Requires("Validation.emailValidator")
>
> will load a file from "/path/to/scripts/Validation/emailValidator.js"
>
> it will be loaded once, even on subsequent calls.
>
> For requiring a namespace:
>
> Requires("Validation.*")
>
> Will try to find a file "/path/to/scripts/Validation/_nsd.js"
> where you'll have to define the many classes in namespace
>
> for example, a _nsd file could be:
>
> Requires(
> "Validation.modes",
> "Validation.validations",
> "Validation.autoValidator"
> );
>
> For namespace definition:
>
> Namespace("Validation",{
> autoValidator:new Class({
> })
>
> })
>
> //START OF CODE
>
> Asset.extend({
> requiredClass: function(source, properties){
> if(source.test(/^.+\.\*$/)){
> source=source.replace(".*","");
> NameSpace(source,{});
> source+="/_nsd";
> }
>
> source=source.replace(".js","#js").replace(".","/").replace("#js",".js");
> if(Window.requiredPath){
> source=Window.requiredPath+source;
> }
> if(!source.contains(".js")){
> source=source+".js";
> }
> var scripts=$$("document head script");
> Window.loadedClasses=Window.loadedClasses || [];
> var sourceId=source.replace(".js","").clean().toLowerCase();
> $each(scripts,function(script){
>
> Window.loadedClasses.include(script.get("src").clean().toLowerCase());
>
> });
> if (!Window.loadedClasses.contains(sourceId)) {
> var a = new Request({
> url: source,
> async: false,
> evalResponse: true,
> method: "get"
> }).send();
> Window.loadedClasses.include(sourceId);
> return Asset.javascript(source, properties);
> }
> }});
>
> function Requires(){
> $A(arguments).each(function(path){
> Asset.requiredClass(path);
> });}
>
> function NameSpace(namespace, definitions){
> var namespaceSplit=namespace.clean().split(".");
> var obj=window;
> for(var i=0; i<namespaceSplit.length;i++){
> var piece=namespaceSplit[i].clean();
> obj[piece]=$pick(obj[piece],{});
> obj=obj[piece];
> }
> $extend(obj,definitions || {});
>
> }