Skip to content

Instantly share code, notes, and snippets.

@paulfalgout
Created March 15, 2016 21:23
Show Gist options
  • Save paulfalgout/2e0975947408344fa425 to your computer and use it in GitHub Desktop.
Save paulfalgout/2e0975947408344fa425 to your computer and use it in GitHub Desktop.
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); // Template Cache
// --------------
var _underscore = require('underscore');
var _underscore2 = _interopRequireDefault(_underscore);
var _backbone = require('backbone');
var _backbone2 = _interopRequireDefault(_backbone);
var _error = require('./error');
var _error2 = _interopRequireDefault(_error);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var TemplateCache = function () {
function TemplateCache(templateId) {
_classCallCheck(this, TemplateCache);
this.templateId = templateId;
}
_createClass(TemplateCache, [{
key: 'load',
// Internal method to load the template
value: function load(options) {
// Guard clause to prevent loading this template more than once
if (this.compiledTemplate) {
return this.compiledTemplate;
}
// Load the template and compile it
var template = this.loadTemplate(this.templateId, options);
this.compiledTemplate = this.compileTemplate(template, options);
return this.compiledTemplate;
}
// Load a template from the DOM, by default. Override
// this method to provide your own template retrieval
// For asynchronous loading with AMD/RequireJS, consider
// using a template-loader plugin as described here:
// https://github.com/marionettejs/backbone.marionette/wiki/Using-marionette-with-requirejs
}, {
key: 'loadTemplate',
value: function loadTemplate(templateId, options) {
var $template = _backbone2.default.$(templateId);
if (!$template.length) {
throw new _error2.default({
name: 'NoTemplateError',
message: 'Could not find template: "' + templateId + '"'
});
}
return $template.html();
}
// Pre-compile the template before caching it. Override
// this method if you do not need to pre-compile a template
// (JST / RequireJS for example) or if you want to change
// the template engine used (Handebars, etc).
}, {
key: 'compileTemplate',
value: function compileTemplate(rawTemplate, options) {
return _underscore2.default.template(rawTemplate, options);
}
}], [{
key: 'get',
// Get the specified template by id. Either
// retrieves the cached version, or loads it
// from the DOM.
value: function get(templateId, options) {
var cachedTemplate = this.templateCaches[templateId];
if (!cachedTemplate) {
cachedTemplate = new TemplateCache(templateId);
this.templateCaches[templateId] = cachedTemplate;
}
return cachedTemplate.load(options);
}
// Clear templates from the cache. If no arguments
// are specified, clears all templates:
// `clear()`
//
// If arguments are specified, clears each of the
// specified templates from the cache:
// `clear("#t1", "#t2", "...")`
}, {
key: 'clear',
value: function clear() {
var i;
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var length = args.length;
if (length > 0) {
for (i = 0; i < length; i++) {
delete this.templateCaches[args[i]];
}
} else {
this.templateCaches = {};
}
}
}, {
key: 'templateCaches',
get: function get() {
if (!this._templateCaches) {
this._templateCaches = {};
}
return this._templateCaches;
},
set: function set(cache) {
this._templateCaches = cache;
}
}]);
return TemplateCache;
}();
exports.default = TemplateCache;
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _underscore = require('underscore');
var _underscore2 = _interopRequireDefault(_underscore);
var _backbone = require('backbone');
var _backbone2 = _interopRequireDefault(_backbone);
var _error = require('./error');
var _error2 = _interopRequireDefault(_error);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Manage templates stored in `<script>` blocks,
// caching them for faster access.
var TemplateCache = function TemplateCache(templateId) {
this.templateId = templateId;
};
// TemplateCache object-level methods. Manage the template
// caches from these method calls instead of creating
// your own TemplateCache instances
// Template Cache
// --------------
_underscore2.default.extend(TemplateCache, {
templateCaches: {},
// Get the specified template by id. Either
// retrieves the cached version, or loads it
// from the DOM.
get: function get(templateId, options) {
var cachedTemplate = this.templateCaches[templateId];
if (!cachedTemplate) {
cachedTemplate = new TemplateCache(templateId);
this.templateCaches[templateId] = cachedTemplate;
}
return cachedTemplate.load(options);
},
// Clear templates from the cache. If no arguments
// are specified, clears all templates:
// `clear()`
//
// If arguments are specified, clears each of the
// specified templates from the cache:
// `clear("#t1", "#t2", "...")`
clear: function clear() {
var i;
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var length = args.length;
if (length > 0) {
for (i = 0; i < length; i++) {
delete this.templateCaches[args[i]];
}
} else {
this.templateCaches = {};
}
}
});
// TemplateCache instance methods, allowing each
// template cache object to manage its own state
// and know whether or not it has been loaded
_underscore2.default.extend(TemplateCache.prototype, {
// Internal method to load the template
load: function load(options) {
// Guard clause to prevent loading this template more than once
if (this.compiledTemplate) {
return this.compiledTemplate;
}
// Load the template and compile it
var template = this.loadTemplate(this.templateId, options);
this.compiledTemplate = this.compileTemplate(template, options);
return this.compiledTemplate;
},
// Load a template from the DOM, by default. Override
// this method to provide your own template retrieval
// For asynchronous loading with AMD/RequireJS, consider
// using a template-loader plugin as described here:
// https://github.com/marionettejs/backbone.marionette/wiki/Using-marionette-with-requirejs
loadTemplate: function loadTemplate(templateId, options) {
var $template = _backbone2.default.$(templateId);
if (!$template.length) {
throw new _error2.default({
name: 'NoTemplateError',
message: 'Could not find template: "' + templateId + '"'
});
}
return $template.html();
},
// Pre-compile the template before caching it. Override
// this method if you do not need to pre-compile a template
// (JST / RequireJS for example) or if you want to change
// the template engine used (Handebars, etc).
compileTemplate: function compileTemplate(rawTemplate, options) {
return _underscore2.default.template(rawTemplate, options);
}
});
exports.default = TemplateCache;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment