On May 3, 8:54 am, Christof Donat <[EMAIL PROTECTED]> wrote:
> variable names. You can e.g. do that by encouraging people to use Plugin
> names like java packages, e.g. "org.jquery.ajax".
>
> Christof

Exactly!  And furthermore if you use a corresponding directory
structure i.e. /jslib/org/jquery/ajax.js then you should eliminate
collisions right off the bat.  This is SOP with the JSAN development I
have been doing for the past year or so.

It also seems the OpenAjax approach assumes an HTML page with a large
number of script tags.  In my apps I use two:
<script src="/jslib/jquery-latest.pack.js" ...></script>
<script src="/jslib/jqjsan.js" ...></script> // the jQuery port of
JSAN i mentioned in a previous post
<script type="text/javascript" ...>
   $.jsanUse('com.myapp.controller');
</script>

And for the sake of example the file /jslib/com/myapp/controller.js
would look something like:
if (typeof self != 'undefined') {
        if (typeof com == "undefined" ){ com = {};}
        if (typeof com.myapp == "undefined" ){ com.myapp = {};}
        if (typeof com.myapp.controller == "undefined" )
{ com.myapp.controller = {

                _dataURL: "data.cgi",
                _logoutURL: "logout.cgi",
                _xmlObj: null,

                initApp: function(){
                        try{
                                
$("a.logoutA").bind("click",com.myapp.controller.logoutEVT);
                                $.ajax({
                                        type: "GET",
                                        url: com.myapp.controller._dataURL,
                                        dataType: "xml",
                                        success: 
com.myapp.controller.dataLoadSuccess,
                                        error: 
com.myapp.controller.connectionFailure
                                 });
                        }catch(theError){
                                $.jsanUse('com.myapp.error');
        
com.myapp.error.catchError("com.myapp.controller.initApp",theError);
                        }
                },

                dataLoadSuccess: function(theResp){
                        try{
                                if(typeof theResp != "undefined"){
                                        com.myapp.controller._xmlObj = (typeof 
theResp.responseXML !=
"undefined")?theResp.responseXML:theResp;
                                        
$(com.myapp.controller._xmlObj).each(function(){
                                                $("error",this).each(function(){
                                                        
$.jsanUse('com.myapp.errorClass');
                                                        
com.myapp.errorClass.doSomething(this);
                                                });
                                                $("user",this).each(function(){
                                                        
$.jsanUse('com.myapp.userClass');
                                                        
com.myapp.userClass.doSomething(this);
                                                });
                                                $("image",this).each(function(){
                                                        
$.jsanUse('com.myapp.imageClass');
                                                        
com.myapp.imageClass.doSomething(this);
                                                });
                                        });
                                }
                        }catch(theError){
                                $.jsanUse('com.myapp.error');
        
com.myapp.error.catchError("com.myapp.controller.dataLoadSuccess",theError);
                        }
                },

                logoutEVT: function(e){
                        try{
                                if (!e){ var e = window.event;}
                                if(typeof e.preventDefault != "undefined")
{e.preventDefault();}else{e.returnValue = false;}
                                theEl = (e.target) ? e.target : e.srcElement;
                                if(theEl){
                                        if (e.type.toLowerCase()=="click") {
                                                $.jsanUse('com.myapp.logout');
                                                com.myapp.logout.doSomething();
                                        }
                                        delete theEl;
                                }
                        }catch(theError){
                                $.jsanUse('com.myapp.error');
        
com.myapp.error.catchError("com.myapp.controller.logoutEVT",theError);
                        }
                },

                connectionFailure: function(){
                        try{
                                /** respond to connection failure **/
                        }catch(theError){
                                $.jsanUse('com.myapp.error');
        
com.myapp.error.catchError("com.myapp.controller.connectionFailure",theError);
                        }
                }


        };}

        $(document).ready(function() {
                try{
                        com.myapp.controller.initApp();
                }catch(theError){
                        $.jsanUse('com.myapp.error');
                        
com.myapp.error.catchError("$(document).ready",theError);
                }
        });

} else if(typeof $.jsanUse != 'undefined'){
        $.jsanUse('com.myapp.controller');
} else {
        throw new Error("com.myapp.controller does not support your
platform");
}

Note that functionality like com.myapp.errorClass is loaded on demand
only if and when I need it.  This keeps the initial footprint small.
Since the namespace is created when the package is loaded any further
calls to $.jsanUse('com.myapp.errorClass') will not reload the files
as it checks for the namespace first.  This approach also encourages
the development of small targeted js files instead of the 30k+
monsters that seem to be so common these days.  Overall your jslib may
contain a larger filesize but you will only be loading small bits
spread over the time your app is being used.

My point here with this over sized (my apologies) example is that many
of the solutions being proposed in the JavaScript world right now
assume a style of coding without explicitly outlining it.  I have not
seen much "if you code like x, y, z our solution will work great for
you" (I refer specifically to online and not printed materials) and I
think that outlining methodologies would really improve the state of
development with this language.  Until such time I would say we should
use caution when deciding to incorporate one approach or another into
a core library.

-wade

Reply via email to