Forums | Mahara Community

Developers /
Javascript namespace isolation


anonymous profile picture
Account deleted
Posts: 16

25 September 2009, 17:20

This might already be on your radar scope ... and I know it would be quite a bit of work to refactor and test ... but at some point for us to play with the Web 2.0 mashup world it will be important to implement namespace isolation in the javascript.  I ran into a small need for this while working on an institutiontype list for pieforms mirroring the authlist element.  Out of the box, mirroring the javascript would cause problems.

Off course, shared javascript should be under mahara or "core" depending on whatever preference Catalyst prefers.

As an example ... this snippet of the existing javascript would be published into the page by authlist ...

    function move_up(id) {
        instanceArray = document.getElementById('instancePriority').value.split(',');
        var outputArray = new Array();
        for(i = instanceArray.length - 1; i >= 0; i--) {
            if(instanceArray[i] == id) {
                outputArray[i] = instanceArray[i-1];
                outputArray[i-1] = instanceArray[i];
                --i;
            } else {
                outputArray[i] = instanceArray[i];
            }
        }
        rebuildInstanceList(outputArray);
    }
 

As an example of namespace isolating ... this snippet from my institutiontypelist javascript would minimize conflicts (except for the case of pages want more than one of this type element ... but this is not such a pieform element).

    institutiontype = new function(){
        this.moduleId = "institutiontype";
        this.version = "1.0.0";
        this.versionDate = "24 September 2009";
        this.codeStatus = "stable";
        this.lastAuthor = "Ray Merrill";
        this.copyright = "Copyright 2009 Accredited Portfolios LLC";
        this.license = "open source GNU GPLv3";
        this.doNothing = function() {};
    }

    institutiontype.move_up = function(id) {
        instanceArray = document.getElementById('instancePriority').value.split(',');
        var outputArray = new Array();
        for(i = instanceArray.length - 1; i >= 0; i--) {
            if(instanceArray[i] == id) {
                outputArray[i] = instanceArray[i-1];
                outputArray[i-1] = instanceArray[i];
                --i;
            } else {
                outputArray[i] = instanceArray[i];
            }
        }
        institutiontype.rebuildInstanceList(outputArray);
    }
 

anonymous profile picture
Account deleted
Posts: 1643

27 September 2009, 17:58

I happen to know that particular piece of javascript isn't the best Smile. So far, it hasn't been something we've thought about, because MochiKit is good about not polluting the global space (or overriding object prototypes), and as such we've been free to do what we like.

In theory, it doesn't matter what we do right? We are the application after all, rather than a library to be used elsewhere Smile. But I agree that in that particular case, where it's a form element with javascript that should be re-usable, it should be namespaced in some way.

Regarding the actual namespace mechanism, we did some namespace-y stuff in files like js/views.js I think. We didn't bother with most of those fields you have, there's not much point in specifying them as you won't ever want them in code - you could just whack 'em in a comment and be done with it Cool 

2 results