templatecache.js

Maintainability

73.09

Lines of code

88

Created with Raphaël 2.1.002550751002013-2-252013-1-262012-12-272012-11-272012-10-282013-3-27Maintainability: 73.09

Created with Raphaël 2.1.00244771942013-2-252013-1-262012-12-272012-11-272012-10-282013-3-27Lines of Code: 88

Difficulty

34.33

Estimated Errors

0.37

Function weight

By Complexity

Created with Raphaël 2.1.0clear3

By SLOC

Created with Raphaël 2.1.0clear13
1
// Template Cache
2
// --------------
3
 
4
// Manage templates stored in `<script>` blocks,
5
// caching them for faster access.
6
Marionette.TemplateCache = function(templateId){
7
  this.templateId = templateId;
8
};
9
 
10
// TemplateCache object-level methods. Manage the template
11
// caches from these method calls instead of creating 
12
// your own TemplateCache instances
13
_.extend(Marionette.TemplateCache, {
14
  templateCaches: {},
15
 
16
  // Get the specified template by id. Either
17
  // retrieves the cached version, or loads it
18
  // from the DOM.
19
  get: function(templateId){
20
    var cachedTemplate = this.templateCaches[templateId];
21
 
22
    if (!cachedTemplate){
23
      cachedTemplate = new Marionette.TemplateCache(templateId);
24
      this.templateCaches[templateId] = cachedTemplate;
25
    }
26
 
27
    return cachedTemplate.load();
28
  },
29
 
30
  // Clear templates from the cache. If no arguments
31
  // are specified, clears all templates:
32
  // `clear()`
33
  //
34
  // If arguments are specified, clears each of the 
35
  // specified templates from the cache:
36
  // `clear("#t1", "#t2", "...")`
37
  clear: function(){
38
    var i;
39
    var args = slice(arguments);
40
    var length = args.length;
41
 
42
    if (length > 0){
43
      for(i=0; i<length; i++){
44
        delete this.templateCaches[args[i]];
45
      }
46
    } else {
47
      this.templateCaches = {};
48
    }
49
  }
50
});
51
 
52
// TemplateCache instance methods, allowing each
53
// template cache object to manage it's own state
54
// and know whether or not it has been loaded
55
_.extend(Marionette.TemplateCache.prototype, {
56
 
57
  // Internal method to load the template
58
  load: function(){
59
    // Guard clause to prevent loading this template more than once
60
    if (this.compiledTemplate){
61
      return this.compiledTemplate;
62
    }
63
 
64
    // Load the template and compile it
65
    var template = this.loadTemplate(this.templateId);
66
    this.compiledTemplate = this.compileTemplate(template);
67
 
68
    return this.compiledTemplate;
69
  },
70
 
71
  // Load a template from the DOM, by default. Override
72
  // this method to provide your own template retrieval
73
  // For asynchronous loading with AMD/RequireJS, consider
74
  // using a template-loader plugin as described here: 
75
  // https://github.com/marionettejs/backbone.marionette/wiki/Using-marionette-with-requirejs
76
  loadTemplate: function(templateId){
77
    var template = Marionette.$(templateId).html();
78
 
79
    if (!template || template.length === 0){
80
      throwError("Could not find template: '" + templateId + "'", "NoTemplateError");
81
    }
82
 
83
    return template;
84
  },
85
 
86
  // Pre-compile the template before caching it. Override
87
  // this method if you do not need to pre-compile a template
88
  // (JST / RequireJS for example) or if you want to change
89
  // the template engine used (Handebars, etc).
90
  compileTemplate: function(rawTemplate){
91
    return _.template(rawTemplate);
92
  }
93
});