-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
lookup.js
238 lines (207 loc) · 7.29 KB
/
lookup.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
Blaze._globalHelpers = {};
// Documented as Template.registerHelper.
// This definition also provides back-compat for `UI.registerHelper`.
Blaze.registerHelper = function (name, func) {
Blaze._globalHelpers[name] = func;
};
// Also documented as Template.deregisterHelper
Blaze.deregisterHelper = function(name) {
delete Blaze._globalHelpers[name];
};
var bindIfIsFunction = function (x, target) {
if (typeof x !== 'function')
return x;
return Blaze._bind(x, target);
};
// If `x` is a function, binds the value of `this` for that function
// to the current data context.
var bindDataContext = function (x) {
if (typeof x === 'function') {
return function () {
var data = Blaze.getData();
if (data == null)
data = {};
return x.apply(data, arguments);
};
}
return x;
};
Blaze._OLDSTYLE_HELPER = {};
Blaze._getTemplateHelper = function (template, name, tmplInstanceFunc) {
// XXX COMPAT WITH 0.9.3
var isKnownOldStyleHelper = false;
if (template.__helpers.has(name)) {
var helper = template.__helpers.get(name);
if (helper === Blaze._OLDSTYLE_HELPER) {
isKnownOldStyleHelper = true;
} else if (helper != null) {
return wrapHelper(bindDataContext(helper), tmplInstanceFunc);
} else {
return null;
}
}
// old-style helper
if (name in template) {
// Only warn once per helper
if (! isKnownOldStyleHelper) {
template.__helpers.set(name, Blaze._OLDSTYLE_HELPER);
if (! template._NOWARN_OLDSTYLE_HELPERS) {
Blaze._warn('Assigning helper with `' + template.viewName + '.' +
name + ' = ...` is deprecated. Use `' + template.viewName +
'.helpers(...)` instead.');
}
}
if (template[name] != null) {
return wrapHelper(bindDataContext(template[name]), tmplInstanceFunc);
}
}
return null;
};
var wrapHelper = function (f, templateFunc) {
if (typeof f !== "function") {
return f;
}
return function () {
var self = this;
var args = arguments;
return Blaze.Template._withTemplateInstanceFunc(templateFunc, function () {
return Blaze._wrapCatchingExceptions(f, 'template helper').apply(self, args);
});
};
};
Blaze._lexicalBindingLookup = function (view, name) {
var currentView = view;
var blockHelpersStack = [];
// walk up the views stopping at a Spacebars.include or Template view that
// doesn't have an InOuterTemplateScope view as a parent
do {
// skip block helpers views
// if we found the binding on the scope, return it
if (_.has(currentView._scopeBindings, name)) {
var bindingReactiveVar = currentView._scopeBindings[name];
return function () {
return bindingReactiveVar.get();
};
}
} while (! (currentView.__startsNewLexicalScope &&
! (currentView.parentView &&
currentView.parentView.__childDoesntStartNewLexicalScope))
&& (currentView = currentView.parentView));
return null;
};
// templateInstance argument is provided to be available for possible
// alternative implementations of this function by 3rd party packages.
Blaze._getTemplate = function (name, templateInstance) {
if ((name in Blaze.Template) && (Blaze.Template[name] instanceof Blaze.Template)) {
return Blaze.Template[name];
}
return null;
};
Blaze._getGlobalHelper = function (name, templateInstance) {
if (Blaze._globalHelpers[name] != null) {
return wrapHelper(bindDataContext(Blaze._globalHelpers[name]), templateInstance);
}
return null;
};
// Looks up a name, like "foo" or "..", as a helper of the
// current template; the name of a template; a global helper;
// or a property of the data context. Called on the View of
// a template (i.e. a View with a `.template` property,
// where the helpers are). Used for the first name in a
// "path" in a template tag, like "foo" in `{{foo.bar}}` or
// ".." in `{{frobulate ../blah}}`.
//
// Returns a function, a non-function value, or null. If
// a function is found, it is bound appropriately.
//
// NOTE: This function must not establish any reactive
// dependencies itself. If there is any reactivity in the
// value, lookup should return a function.
Blaze.View.prototype.lookup = function (name, _options) {
var template = this.template;
var lookupTemplate = _options && _options.template;
var helper;
var binding;
var boundTmplInstance;
var foundTemplate;
if (this.templateInstance) {
boundTmplInstance = Blaze._bind(this.templateInstance, this);
}
// 0. looking up the parent data context with the special "../" syntax
if (/^\./.test(name)) {
// starts with a dot. must be a series of dots which maps to an
// ancestor of the appropriate height.
if (!/^(\.)+$/.test(name))
throw new Error("id starting with dot must be a series of dots");
return Blaze._parentData(name.length - 1, true /*_functionWrapped*/);
}
// 1. look up a helper on the current template
if (template && ((helper = Blaze._getTemplateHelper(template, name, boundTmplInstance)) != null)) {
return helper;
}
// 2. look up a binding by traversing the lexical view hierarchy inside the
// current template
if (template && (binding = Blaze._lexicalBindingLookup(Blaze.currentView, name)) != null) {
return binding;
}
// 3. look up a template by name
if (lookupTemplate && ((foundTemplate = Blaze._getTemplate(name, boundTmplInstance)) != null)) {
return foundTemplate;
}
// 4. look up a global helper
if ((helper = Blaze._getGlobalHelper(name, boundTmplInstance)) != null) {
return helper;
}
// 5. look up in a data context
return function () {
var isCalledAsFunction = (arguments.length > 0);
var data = Blaze.getData();
var x = data && data[name];
if (! x) {
if (lookupTemplate) {
throw new Error("No such template: " + name);
} else if (isCalledAsFunction) {
throw new Error("No such function: " + name);
} else if (name.charAt(0) === '@' && ((x === null) ||
(x === undefined))) {
// Throw an error if the user tries to use a `@directive`
// that doesn't exist. We don't implement all directives
// from Handlebars, so there's a potential for confusion
// if we fail silently. On the other hand, we want to
// throw late in case some app or package wants to provide
// a missing directive.
throw new Error("Unsupported directive: " + name);
}
}
if (! data) {
return null;
}
if (typeof x !== 'function') {
if (isCalledAsFunction) {
throw new Error("Can't call non-function: " + x);
}
return x;
}
return x.apply(data, arguments);
};
};
// Implement Spacebars' {{../..}}.
// @param height {Number} The number of '..'s
Blaze._parentData = function (height, _functionWrapped) {
// If height is null or undefined, we default to 1, the first parent.
if (height == null) {
height = 1;
}
var theWith = Blaze.getView('with');
for (var i = 0; (i < height) && theWith; i++) {
theWith = Blaze.getView(theWith, 'with');
}
if (! theWith)
return null;
if (_functionWrapped)
return function () { return theWith.dataVar.get(); };
return theWith.dataVar.get();
};
Blaze.View.prototype.lookupTemplate = function (name) {
return this.lookup(name, {template:true});
};