PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/static/scripts/template.js

https://github.com/dbrgn/gramophone
JavaScript | 37 lines | 18 code | 6 blank | 13 comment | 2 complexity | ccbff88d1409b836fbf464c871933f24 MD5 | raw file
  1. (function($) {
  2. var cache = {};
  3. function _render(elt, template, data, callback) {
  4. var data = data || {},
  5. callback = callback || function() {},
  6. html = template(data);
  7. elt.html(html);
  8. callback();
  9. }
  10. /**
  11. * Fetches the Underscore.js template at the given path,
  12. * processes it with the provided obj, and appends the
  13. * resulting html to the matched DOM elements.
  14. *
  15. * Templates will only be fetched once from the server,
  16. * after which the preprocessed template will be cached
  17. * in the DOM.
  18. */
  19. $.fn.template = function(path, obj, callback) {
  20. var self = this;
  21. /*if (cache[path]) {*/
  22. /*_render(self, cache[path], obj, callback);*/
  23. /*return self;*/
  24. /*}*/
  25. $.get(path, function(data) {
  26. cache[path] = _.template(data);
  27. _render(self, cache[path], obj, callback);
  28. });
  29. return self;
  30. };
  31. })(jQuery);