/src/main/webapp/js/ext/template.js
JavaScript | 36 lines | 22 code | 6 blank | 8 comment | 3 complexity | e4c863f6440263d72e84a7f57eae09af MD5 | raw file
- (function($) {
- var cache = {};
-
- function _render(elt, template, data, callback) {
- var data = data || {},
- callback = callback || function() {},
- html = template(data);
-
- elt.append(html);
- callback();
- }
-
- /**
- * Fetches the Underscore.js template at the given path,
- * processes it with the provided data object, and appends the
- * resulting html to the matched DOM elements.
- *
- * Templates will only be fetched once from the server,
- * preprocessed template are cached in the DOM.
- */
- $.fn.template = function(path, obj, callback) {
- var self = this;
-
- if (cache[path]) {
- _render(self, cache[path], obj, callback);
- return self;
- }
-
- $.get(path, function(data) {
- cache[path] = _.template(data);
- _render(self, cache[path], obj, callback);
- });
-
- return self;
- };
- })(jQuery);