getify / LABjs

Loading And Blocking JavaScript: On-demand parallel loader for JavaScript with execution order dependencies
http://labjs.com
2.28k stars 313 forks source link

Ability to add chained cachebust #97

Closed asnyder closed 10 years ago

asnyder commented 10 years ago

I know you don't want to add cache busting and recommend creating a wrapper method, however, by creating a wrapper method I can't chain my LABJS, for example:

Trying to do : $LAB.script_cb = function(path) {return this.script(path + '?v{{last_build_date}}') will not work as .script will return an array of script, and wait only. So My ideal would be to use script_cb as a swap in certain places for .script. I tried to access the object literal that houses those functions but it doesn't seem accessible from the outside.

Furthermore, not being able to chain adds some overhead as I have to constantly repeat the $LAB lines, instead of just .script_sb, .wait, etc.

I would settle for a way to add script_cb so it returns as one of the methods returned from .script so it can chain. Any ideas or suggestions?

Thanks so much for your help.

asnyder commented 10 years ago

Alternatively, I can wrap the string in a global function and always call something like .script(_cb(...)), but that seems bad.

getify commented 10 years ago

I know you don't want to add cache busting

See: CacheBust option here: http://labjs.com/documentation.php#setglobaldefaults

I don't have a problem with LABjs truly cache busting, with random numbers. What I don't think LABjs should be doing is munging URLs with higher level abstractions like version numbers. To me that still seems like it should be handled by your build process, or your templating engine, or ... etc.

Trying to do : $LAB.script_cb = function(path) {return this.script(path + '?v{{last_build_date}}') will not work

Correct, it won't, as you provided it, return your wrapper each time, it'll just return the $LAB chain.

But with a little extra work, you should be able to achieve what (I think) you're hoping for:

var $LAB_wrapper = (function(version){
   var $L = $LAB, publicAPI = {
      script: function(src) {
         $L = $L.script(src + "?v" + version);
         return publicAPI;
      },
      wait: function() {
         $L = $L.wait.apply(null,arguments);
         return publicAPI;
      }
   };
   return publicAPI;
})("{{last_build_date}}");

// later

$LAB_wrapper.script("foo.js").wait().("bar.js");
// loads "foo.js?v1234" and "bar.js?v1234"

Hopefully that helps get you on the path you're looking for. :)