-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwig-decorator.ts
60 lines (51 loc) · 1.29 KB
/
twig-decorator.ts
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
import {
Component
} from '@angular/core';
import {twig} from 'twig';
declare var Reflect;
export function Twig(args:any={}) {
return function TwigDecorator(cls) {
args.template = RenderSync(args);
TwigComponent(args)(cls);
};
}
function loadTemplate(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false); // MUST BE SYNC FOR NOW!!!
xhr.send();
return xhr.responseText;
}
function TwigComponent(config) {
return function(cls) {
var annotations = Reflect.getMetadata('annotations', cls) || [];
annotations.push(new Component(config));
Reflect.defineMetadata('annotations', annotations, cls);
return cls;
}
}
function RenderSync(args) {
let tpl = args.template;
if(args.templateUrl) {
tpl = loadTemplate(args.templateUrl);
}
(<any>console).log('annotation Twig OK');
return twig({
id: 'RenderSync',
data: tpl
}).render(args.context);
}
// Not working for now because decorators are resolved synchronously!
function RenderAsync(args) {
return new Promise( (resolve, reject) => {
twig({
id: 'RenderAsync',
href: args.templateUrl,
load: (template) => {
let tpl = template.render(args.context);
console.log('annotation Twig OK');
resolve(tpl);
},
error: reject
});
});
}