forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspacebars-runtime.js
277 lines (244 loc) · 9.12 KB
/
spacebars-runtime.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
Spacebars = {};
var tripleEquals = function (a, b) { return a === b; };
Spacebars.include = function (templateOrFunction, contentFunc, elseFunc) {
if (! templateOrFunction)
return null;
if (typeof templateOrFunction !== 'function') {
var template = templateOrFunction;
if (! Blaze.isTemplate(template))
throw new Error("Expected template or null, found: " + template);
return templateOrFunction.constructView(contentFunc, elseFunc);
}
var templateVar = Blaze.ReactiveVar(null, tripleEquals);
var view = Blaze.View('Spacebars.include', function () {
var template = templateVar.get();
if (template === null)
return null;
if (! Blaze.isTemplate(template))
throw new Error("Expected template or null, found: " + template);
return template.constructView(contentFunc, elseFunc);
});
view.__templateVar = templateVar;
view.onViewCreated(function () {
this.autorun(function () {
templateVar.set(templateOrFunction());
});
});
return view;
};
// Executes `{{foo bar baz}}` when called on `(foo, bar, baz)`.
// If `bar` and `baz` are functions, they are called before
// `foo` is called on them.
//
// This is the shared part of Spacebars.mustache and
// Spacebars.attrMustache, which differ in how they post-process the
// result.
Spacebars.mustacheImpl = function (value/*, args*/) {
var args = arguments;
// if we have any arguments (pos or kw), add an options argument
// if there isn't one.
if (args.length > 1) {
var kw = args[args.length - 1];
if (! (kw instanceof Spacebars.kw)) {
kw = Spacebars.kw();
// clone arguments into an actual array, then push
// the empty kw object.
args = Array.prototype.slice.call(arguments);
args.push(kw);
} else {
// For each keyword arg, call it if it's a function
var newHash = {};
for (var k in kw.hash) {
var v = kw.hash[k];
newHash[k] = (typeof v === 'function' ? v() : v);
}
args[args.length - 1] = Spacebars.kw(newHash);
}
}
return Spacebars.call.apply(null, args);
};
Spacebars.mustache = function (value/*, args*/) {
var result = Spacebars.mustacheImpl.apply(null, arguments);
if (result instanceof Spacebars.SafeString)
return HTML.Raw(result.toString());
else
// map `null`, `undefined`, and `false` to null, which is important
// so that attributes with nully values are considered absent.
// stringify anything else (e.g. strings, booleans, numbers including 0).
return (result == null || result === false) ? null : String(result);
};
Spacebars.attrMustache = function (value/*, args*/) {
var result = Spacebars.mustacheImpl.apply(null, arguments);
if (result == null || result === '') {
return null;
} else if (typeof result === 'object') {
return result;
} else if (typeof result === 'string' && HTML.isValidAttributeName(result)) {
var obj = {};
obj[result] = '';
return obj;
} else {
throw new Error("Expected valid attribute name, '', null, or object");
}
};
Spacebars.dataMustache = function (value/*, args*/) {
var result = Spacebars.mustacheImpl.apply(null, arguments);
return result;
};
// Idempotently wrap in `HTML.Raw`.
//
// Called on the return value from `Spacebars.mustache` in case the
// template uses triple-stache (`{{{foo bar baz}}}`).
Spacebars.makeRaw = function (value) {
if (value == null) // null or undefined
return null;
else if (value instanceof HTML.Raw)
return value;
else
return HTML.Raw(value);
};
// If `value` is a function, called it on the `args`, after
// evaluating the args themselves (by calling them if they are
// functions). Otherwise, simply return `value` (and assert that
// there are no args).
Spacebars.call = function (value/*, args*/) {
if (typeof value === 'function') {
// evaluate arguments if they are functions (by calling them)
var newArgs = [];
for (var i = 1; i < arguments.length; i++) {
var arg = arguments[i];
newArgs[i-1] = (typeof arg === 'function' ? arg() : arg);
}
return value.apply(null, newArgs);
} else {
if (arguments.length > 1)
throw new Error("Can't call non-function: " + value);
return value;
}
};
// Call this as `Spacebars.kw({ ... })`. The return value
// is `instanceof Spacebars.kw`.
Spacebars.kw = function (hash) {
if (! (this instanceof Spacebars.kw))
// called without new; call with new
return new Spacebars.kw(hash);
this.hash = hash || {};
};
// Call this as `Spacebars.SafeString("some HTML")`. The return value
// is `instanceof Spacebars.SafeString` (and `instanceof Handlebars.SafeString).
Spacebars.SafeString = function (html) {
if (! (this instanceof Spacebars.SafeString))
// called without new; call with new
return new Spacebars.SafeString(html);
return new Handlebars.SafeString(html);
};
Spacebars.SafeString.prototype = Handlebars.SafeString.prototype;
// `Spacebars.dot(foo, "bar", "baz")` performs a special kind
// of `foo.bar.baz` that allows safe indexing of `null` and
// indexing of functions (which calls the function). If the
// result is a function, it is always a bound function (e.g.
// a wrapped version of `baz` that always uses `foo.bar` as
// `this`).
//
// In `Spacebars.dot(foo, "bar")`, `foo` is assumed to be either
// a non-function value or a "fully-bound" function wrapping a value,
// where fully-bound means it takes no arguments and ignores `this`.
//
// `Spacebars.dot(foo, "bar")` performs the following steps:
//
// * If `foo` is falsy, return `foo`.
//
// * If `foo` is a function, call it (set `foo` to `foo()`).
//
// * If `foo` is falsy now, return `foo`.
//
// * Return `foo.bar`, binding it to `foo` if it's a function.
Spacebars.dot = function (value, id1/*, id2, ...*/) {
if (arguments.length > 2) {
// Note: doing this recursively is probably less efficient than
// doing it in an iterative loop.
var argsForRecurse = [];
argsForRecurse.push(Spacebars.dot(value, id1));
argsForRecurse.push.apply(argsForRecurse,
Array.prototype.slice.call(arguments, 2));
return Spacebars.dot.apply(null, argsForRecurse);
}
if (typeof value === 'function')
value = value();
if (! value)
return value; // falsy, don't index, pass through
var result = value[id1];
if (typeof result !== 'function')
return result;
// `value[id1]` (or `value()[id1]`) is a function.
// Bind it so that when called, `value` will be placed in `this`.
return function (/*arguments*/) {
return result.apply(value, arguments);
};
};
// Spacebars.With implements the conditional logic of rendering
// the `{{else}}` block if the argument is falsy. It combines
// a Blaze.If with a Blaze.With (the latter only in the truthy
// case, since the else block is evaluated without entering
// a new data context).
Spacebars.With = function (argFunc, contentFunc, elseFunc) {
var argVar = new Blaze.ReactiveVar;
var view = Blaze.View('Spacebars_with', function () {
return Blaze.If(function () { return argVar.get(); },
function () { return Blaze.With(function () {
return argVar.get(); }, contentFunc); },
elseFunc);
});
view.onViewCreated(function () {
this.autorun(function () {
argVar.set(argFunc());
// This is a hack so that autoruns inside the body
// of the #with get stopped sooner. It reaches inside
// our ReactiveVar to access its dep.
Tracker.onInvalidate(function () {
argVar.dep.changed();
});
// Take the case of `{{#with A}}{{B}}{{/with}}`. The goal
// is to not re-render `B` if `A` changes to become falsy
// and `B` is simultaneously invalidated.
//
// A series of autoruns are involved:
//
// 1. This autorun (argument to Spacebars.With)
// 2. Argument to Blaze.If
// 3. Blaze.If view re-render
// 4. Argument to Blaze.With
// 5. The template tag `{{B}}`
//
// When (3) is invalidated, it immediately stops (4) and (5)
// because of a Tracker.onInvalidate built into materializeView.
// (When a View's render method is invalidated, it immediately
// tears down all the subviews, via a Tracker.onInvalidate much
// like this one.
//
// Suppose `A` changes to become falsy, and `B` changes at the
// same time (i.e. without an intervening flush).
// Without the code above, this happens:
//
// - (1) and (5) are invalidated.
// - (1) runs, invalidating (2) and (4).
// - (5) runs.
// - (2) runs, invalidating (3), stopping (4) and (5).
//
// With the code above:
//
// - (1) and (5) are invalidated, invalidating (2) and (4).
// - (1) runs.
// - (2) runs, invalidating (3), stopping (4) and (5).
//
// If the re-run of (5) is originally enqueued before (1), all
// bets are off, but typically that doesn't seem to be the
// case. Anyway, doing this is always better than not doing it,
// because it might save a bunch of DOM from being updated
// needlessly.
});
});
return view;
};
// XXX COMPAT WITH 0.9.0
Spacebars.TemplateWith = Blaze._TemplateWith;