code_junkie

Lazy Load External Javascript Files

Nov 13th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. var scriptResource = new ScriptResource('location/of/my/script.js');
  2. scriptResource.call('methodName', arg1, arg2);
  3.  
  4. var scriptResource = new ScriptResource('location/of/my/script.js');
  5. scriptResource.methodName(arg1, arg2);
  6.  
  7. ScriptResource = function(scriptLocation)
  8. {
  9. this.onMethodCall = function(methodName)
  10. {
  11. this.call(arguments);
  12. }
  13. }
  14.  
  15. obj.__noSuchMethod__ = funtion( id, args ) {
  16. this[id].apply( this, args );
  17. }
  18.  
  19. var methods = ["foo", "bar", "baz"];
  20. for (var i=0; i<methods.length; i++) {
  21. var method_name = methods[i];
  22. WildCardMethodHandler[method_name] = function () {
  23. this.handleAllMethods(method_name);
  24. };
  25. }
  26.  
  27. var extObj = ScriptResource('location/of/my/script.js');
  28. extObj('methodname')(arg1,arg2);
  29.  
  30. function ScriptResource(file) {
  31. return function(method) {
  32. loadExternalScript(file);
  33. return window[method];
  34. }
  35. }
Add Comment
Please, Sign In to add comment