This repository was archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlazy-compile-directive.js
60 lines (54 loc) · 1.63 KB
/
lazy-compile-directive.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
/*!
* Lazy compile directive.
*
* Copyright (c) 2014-2017 Digital Bazaar, Inc. All rights reserved.
*
* @author Dave Longley
*/
/* @ngInject */
export default function factory($compile, $templateCache) {
return {
restrict: 'A',
// run before just about anything else
priority: 2000,
terminal: true,
compile: Compile
};
function Compile(tElement, tAttrs) {
var contents = tElement.contents();
// TODO: automate ID generation and remove need to have user specify
// use template cache for contents if requested
if('brLazyId' in tAttrs) {
var cacheId = 'br-lazy-compile-id:' + tAttrs.brLazyId;
var cached = $templateCache.get(cacheId);
if(cached === undefined) {
$templateCache.put(cacheId, contents);
} else {
// use cached contents (allow local contents to be GC'd)
contents = cached;
}
}
tElement.empty();
return function(scope, element, attrs, ctrls, transcludeFn) {
// bind-once to br-lazy-compile=<expression>
var removeWatch = scope.$watch(tAttrs.brLazyCompile, function(value) {
if(value) {
removeWatch();
compile();
}
});
function compile() {
// add contents back
// TODO: avoid clone, if possible, when not using template cache
element.append(contents.clone());
// avoid recursion during compile
element.removeAttr('br-lazy-compile');
element.removeAttr('br-lazy-id');
// recompile element and link
$compile(element)(scope, undefined, {
parentBoundTranscludeFn: transcludeFn
});
}
};
}
}