This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
forked from jfparadis/requirejs-handlebars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhbars.js
81 lines (73 loc) · 2.91 KB
/
hbars.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// RequireJS Handlebars template plugin
// http://github.com/jfparadis/requirejs-handlebars
//
// An alternative to http://github.com/SlexAxton/require-handlebars-plugin/blob/master/hbs.js
//
// Using Handlebars Semantic templates at http://handlebarsjs.com
// Using and RequireJS text.js at http://requirejs.org/docs/api.html#text
// @author JF Paradis
// @version 0.0.2
//
// Released under the MIT license
//
// Usage:
// require(['backbone', 'hbar!mytemplate'], function (Backbone, mytemplate) {
// return Backbone.View.extend({
// initialize: function(){
// this.render();
// },
// render: function(){
// this.$el.html(mytemplate({message: 'hello'}));
// });
// });
//
// Configuration: (optional)
// require.config({
// hbars: {
// extension: '.hbar' // default = '.html'
// }
// });
/*jslint nomen: true */
/*global define: false */
define(['text', 'Handlebars'], function (text, Handlebars) {
'use strict';
var buildMap = {},
buildTemplateSource = "define('{pluginName}!{moduleName}', ['Handlebars'], function (Handlebars) { return Handlebars.template({content}); });\n";
return {
version: '0.0.2',
load: function (moduleName, parentRequire, onload, config) {
if (buildMap[moduleName]) {
onload(buildMap[moduleName]);
} else {
var ext = (config.hbars && config.hbars.extension) || '.html',
path = (config.hbars && config.hbars.path) || '',
compileOptions = (config.hbars && config.hbars.compileOptions) || {},
textOnload = function (source) {
if (config.isBuild) {
// We store the precompiled template so we can use the
// handlebars.runtime after build.
buildMap[moduleName] = Handlebars.precompile(source, compileOptions);
// Don't bother doing anything else during build.
onload();
} else {
// We store the compiled template for reuse
buildMap[moduleName] = Handlebars.compile(source);
onload(buildMap[moduleName]);
}
};
textOnload.error = onload.error;
text.load(path + moduleName + ext, parentRequire, textOnload, config);
}
},
write: function (pluginName, moduleName, write, config) {
var content = buildMap[moduleName];
if (content) {
write.asModule(pluginName + '!' + moduleName,
buildTemplateSource
.replace('{pluginName}', pluginName)
.replace('{moduleName}', moduleName)
.replace('{content}', content));
}
}
};
});