forked from peerlibrary/meteor-blaze-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.js
135 lines (119 loc) · 4.04 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
/* This file is a direct copy of Blaze lookup.js with modifications described
in this pull request: https://github.com/meteor/meteor/pull/4036
TODO: Remove this file once the pull request is merged in.
*/
// 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._getTemplateHelper = function (template, name, templateInstance) {
// 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), templateInstance);
} 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]), templateInstance);
}
}
return null;
};
var wrapHelper = function (f, templateFunc) {
// XXX COMPAT WITH METEOR 1.0.3.2
if (! Blaze.Template._withTemplateInstanceFunc) {
return Blaze._wrapCatchingExceptions(f, 'template helper');
}
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);
});
};
};
// 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.View.prototype.lookup = function (name, _options) {
var template = this.template;
var lookupTemplate = _options && _options.template;
var helper;
var boundTmplInstance;
var foundTemplate;
if (this.templateInstance) {
boundTmplInstance = _.bind(this.templateInstance, this);
}
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*/);
} else if (template &&
((helper = Blaze._getTemplateHelper(template, name, boundTmplInstance)) != null)) {
return helper;
} else if (lookupTemplate &&
((foundTemplate = Blaze._getTemplate(name, boundTmplInstance)) != null)) {
return foundTemplate;
} else if (Blaze._globalHelpers[name] != null) {
return wrapHelper(bindDataContext(Blaze._globalHelpers[name]),
boundTmplInstance);
} else {
return function () {
var isCalledAsFunction = (arguments.length > 0);
var data = Blaze.getData();
if (lookupTemplate && ! (data && data[name])) {
throw new Error("No such template: " + name);
}
if (isCalledAsFunction && ! (data && data[name])) {
throw new Error("No such function: " + name);
}
if (! data)
return null;
var x = data[name];
if (typeof x !== 'function') {
if (isCalledAsFunction) {
throw new Error("Can't call non-function: " + x);
}
return x;
}
return x.apply(data, arguments);
};
}
return null;
};