Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/angular2/src/core/annotations/template_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@ import {List} from 'angular2/src/facade/collection';

export class TemplateConfig {
url:any; //string;
cssUrl:any; //string
inline:any; //string;
directives:any; //List<Type>;
formatters:any; //List<Type>;
source:any;//List<TemplateConfig>;
@CONST()
constructor({
url,
cssUrl,
inline,
directives,
formatters,
source
}: {
url: string,
cssUrl: string,
inline: string,
directives: List<Type>,
formatters: List<Type>,
source: List<TemplateConfig>
})
{
this.url = url;
this.cssUrl = cssUrl;
this.inline = inline;
this.directives = directives;
this.formatters = formatters;
Expand Down
50 changes: 38 additions & 12 deletions modules/angular2/src/core/compiler/template_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,47 @@ export class TemplateLoader {
return PromiseWrapper.resolve(template);
}

if (isPresent(tplConfig.url) && isPresent(tplConfig.cssUrl)) {
return Promise.all([
this.buildUrl(tplConfig.url),
this.buildUrl(tplConfig.cssUrl)
])
.then((props) => {
// if I had .spread this would be one line
var html = props[0];
var style = props[1];
return this.createTemplateWithCss(html, style);
});
}

if (isPresent(tplConfig.url)) {
var url = tplConfig.url;
var promise = StringMapWrapper.get(this._cache, url);

if (isBlank(promise)) {
promise = this._xhr.get(url).then(function (html) {
var template = DOM.createTemplate(html);
return template;
});
StringMapWrapper.set(this._cache, url, promise);
}

return promise;
return this.buildUrl(tplConfig.url).then(DOM.createTemplate);
}

if (isPresent(tplConfig.cssUrl)) {
return this.buildUrl(tplConfig.cssUrl).then(DOM.createStyleElement);
}

throw new BaseException(`No template configured for component ${stringify(cmpMetadata.type)}`);
}
buildUrl(url):Promise {
var promise = StringMapWrapper.get(this._cache, url);

if (isBlank(promise)) {
promise = this._xhr.get(url)
StringMapWrapper.set(this._cache, url, promise);
}

return promise;
}
createTemplateWithCss(html, css) {
var style = DOM.createStyleElement(css);

var d = DOM.createElement('div');
DOM.setInnerHTML(d, html);
DOM.insertBefore(d.firstChild, style);

return DOM.createTemplate(d.innerHTML);
}

}
7 changes: 6 additions & 1 deletion modules/angular2/src/facade/dom.es6
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ export class DOM {
}
static createStyleElement(css:string, doc=document):StyleElement {
var style = doc.createElement('STYLE');
style.innerText = css;
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(doc.createTextNode(css));
}
return style;
}
static clone(node:Node) {
Expand Down