-
Notifications
You must be signed in to change notification settings - Fork 0
/
templating.js
78 lines (66 loc) · 2.67 KB
/
templating.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
// Packages and apps add templates on to this object.
/**
* @summary The class for defining templates
* @class
* @instanceName Template.myTemplate
*/
Template = Blaze.Template;
var RESERVED_TEMPLATE_NAMES = "__proto__ name".split(" ");
// Check for duplicate template names and illegal names that won't work.
Template.__checkName = function (name) {
// Some names can't be used for Templates. These include:
// - Properties Blaze sets on the Template object.
// - Properties that some browsers don't let the code to set.
// These are specified in RESERVED_TEMPLATE_NAMES.
if (name in Template || _.contains(RESERVED_TEMPLATE_NAMES, name)) {
if ((Template[name] instanceof Template) && name !== "body")
throw new Error("There are multiple templates named '" + name + "'. Each template needs a unique name.");
throw new Error("This template name is reserved: " + name);
}
};
// XXX COMPAT WITH 0.8.3
Template.__define__ = function (name, renderFunc) {
Template.__checkName(name);
Template[name] = new Template("Template." + name, renderFunc);
// Exempt packages built pre-0.9.0 from warnings about using old
// helper syntax, because we can. It's not very useful to get a
// warning about someone else's code (like a package on Atmosphere),
// and this should at least put a bit of a dent in number of warnings
// that come from packages that haven't been updated lately.
Template[name]._NOWARN_OLDSTYLE_HELPERS = true;
};
// Define a template `Template.body` that renders its
// `contentRenderFuncs`. `<body>` tags (of which there may be
// multiple) will have their contents added to it.
/**
* @summary The [template object](#templates_api) representing your `<body>`
* tag.
* @locus Client
*/
Template.body = new Template('body', function () {
var view = this;
return _.map(Template.body.contentRenderFuncs, function (func) {
return func.apply(view);
});
});
Template.body.contentRenderFuncs = []; // array of Blaze.Views
Template.body.view = null;
Template.body.addContent = function (renderFunc) {
Template.body.contentRenderFuncs.push(renderFunc);
};
// This function does not use `this` and so it may be called
// as `Meteor.startup(Template.body.renderIntoDocument)`.
Template.body.renderToDocument = function () {
// Only do it once.
if (Template.body.view)
return;
var view = Blaze.render(Template.body, document.body);
Template.body.view = view;
};
// XXX COMPAT WITH 0.9.0
UI.body = Template.body;
// XXX COMPAT WITH 0.9.0
// (<body> tags in packages built with 0.9.0)
Template.__body__ = Template.body;
Template.__body__.__contentParts = Template.body.contentViews;
Template.__body__.__instantiate = Template.body.renderToDocument;