views:

444

answers:

3

I am trying to write a javascript class that loads script files as they are needed. I have most of this working. It is possible to use the library with the following Syntax:

var scriptResource = new ScriptResource('location/of/my/script.js');
scriptResource.call('methodName', arg1, arg2);

I would like to add some additional syntactic sugar so you could write

var scriptResource = new ScriptResource('location/of/my/script.js');
scriptResource.methodName(arg1, arg2);

I'm almost certain that this isnt possible but there may be an inventive solution. I guess what there need to be is some sort of methodCall event. SO the following could work

ScriptResource = function(scriptLocation)
{
    this.onMethodCall = function(methodName)
    {
        this.call(arguments);
    }
}

This code is obviously very incomplete but I hope it gives an idea of what I am trying to do

Is something like this even remotely possible?

A: 

If the set of method names is limited, then you could generate those methods:

var methods = ["foo", "bar", "baz"];
for (var i=0; i<methods.length; i++) {
    var method_name = methods[i];
    WildCardMethodHandler[method_name] = function () {
        this.handleAllMethods(method_name);
    };
}

edit: Posted this answer before the question changed dramatically.

Rene Saarsoo
I'm afriad this isn't going to work. I need to call any method. I have made a big edit to show the usage I am after.
Jack Ryan
I almost thought I answered to the wrong question. You have made some truly extensive edits.
Rene Saarsoo
Yes. Sorry for the change. I thought it was best to present the question in more concrete terms. I saved edit as your saved you answer. Thanks for having a go at it though.
Jack Ryan
+3  A: 

There is a non standard method, __noSuchMethod__ in firefox that does what you're looking for
have a look at
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/noSuchMethod

so you could define

obj.__noSuchMethod__ = funtion( id, args ) {
    this[id].apply( this, args );
}
meouw
I was going to post the same answer: +1
Christoph
I've just about managed to get this to work. It would be nice if there was a full cross browser solution though.
Jack Ryan
Really not sure how cross browser it is - may work in other mozilla based browsers?I've only used it in our office which is all firefoxAnyone else know? Christoph?
meouw
@meouw: it's not cross browser at all (only works in Geckos); that's why I asked for your current implementation of call() so that we might look for workarounds...
Christoph
A: 

An intermediary solution might be to have syntax such as:

var extObj = ScriptResource('location/of/my/script.js');  
extObj('methodname')(arg1,arg2);

the code might look like this:

function ScriptResource(file) {
  return function(method) {
    loadExternalScript(file);
    return window[method];
  }
}

All kinds of assumptions in the code above, which I'd let you figure out yourself. The most interesting, IMHO, is - in your original implementation - how do you get the proxyied method to run synchronously and return a value? AFAIK you can only load external scripts asynchronously and handle them with an "onload" callback.

Guss
Synchronous AJAX is definitely possible. I'm using it every day. This page explains how: http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX
Rene Saarsoo