~bcsaller/juju-gui/graph-component

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';

var Handlebars = require('yui/handlebars').Handlebars,
    fs = require('fs'),
    cache = {};


function read(path, options, fn) {
  var str = cache[path];

  // cached (only if cached is a string and not a compiled template function)
  if (str && typeof str === 'string') {
    return fn(null, str);
  }

  // read
  fs.readFile(path, 'utf8', function(err, str) {
    if (err) {
      return fn(err);
    }
    if (options.cache) {
      cache[path] = str;
    }
    fn(null, str);
  });
}


exports.handlebars = function(path, options, fn) {
  var tmpl;
  read(path, options, function(err, str) {
    if (err) {
      console.log('error: ', err);
      return fn(err);
    }
    options.filename = path;
    tmpl = Handlebars.compile(str, options);
    fn(null, tmpl(options));
  });
};