UNPKG

marionette.templatecache

Version:

Renderer which provides a cache for retrieving templates from script blocks in your HTML. This improved the speed of subsequent calls to get a template.

142 lines (111 loc) 4.29 kB
/** * @license * MarionetteJS (Marionette.TemplateCache) * ---------------------------------- * v1.0.0 * * Copyright (c)2018 Derick Bailey, Muted Solutions, LLC. * Distributed under MIT license * * http://marionettejs.com */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('underscore'), require('backbone')) : typeof define === 'function' && define.amd ? define(['underscore', 'backbone'], factory) : (global.Marionette = global.Marionette || {}, global.Marionette.TemplateCache = factory(global._,global.Backbone)); }(this, (function (_,Backbone) { 'use strict'; _ = _ && _.hasOwnProperty('default') ? _['default'] : _; Backbone = Backbone && Backbone.hasOwnProperty('default') ? Backbone['default'] : Backbone; // Template Cache // -------------- // 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 _.extend(TemplateCache, { templateCaches: {}, // Render a template with data. The `template` parameter is // passed to the `TemplateCache` object to retrieve the // template function. render: function render(template, data) { if (!template) { throw new Error('Cannot render the template since its false, null or undefined.'); } var templateFunc = _.isFunction(template) ? template : TemplateCache.get(template); return templateFunc(data); }, // 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 = void 0; 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 _.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 = Backbone.$(templateId); if (!$template.length) { throw new Error('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 _.template(rawTemplate, options); } }); return TemplateCache; }))); //# sourceMappingURL=marionette.templatecache.js.map