是否可以通过 Ajax 加载 Handlebars 模板?
-
13-11-2019 - |
题
我想动态加载额外的模板。是否可以?
解决方案
您可以在以下位置注册新模板 Ember.TEMPLATES
. 。然后它们将可供查看。
我的代码摘录(jQuery Ajax 处理程序):
success: function(data) {
$(data).filter('script[type="text/x-handlebars"]').each(function() {
templateName = $(this).attr('data-template-name');
Ember.TEMPLATES[templateName] = Ember.Handlebars.compile($(this).html());
});
}
就是这样。
其他提示
我只是在寻找同样的事情,我即将在下面的赛段玩得分
信用:Borismus在github上 https://gist.github.com/2165681
<script>
/*
* Loads a handlebars.js template at a given URL. Takes an optional name, in which case,
* the template is added and is reference-able via templateName.
*/
function loadTemplate(url, name, callback) {
var contents = $.get(url, function(templateText) {
var compiledTemplate = Ember.Handlebars.compile(templateText);
if (name) {
Ember.TEMPLATES[name] = compiledTemplate
} else {
Ember.View.create({ template: compiledTemplate }).append();
}
if (callback) {
callback();
}
});
}
</script>
. 我正在使用RequireJs以及文本插件动态加载车把模板。
r.js优化器将处理绑定栏模板到文本文件,可以使用requirejs甚至ajax轻松加载
不隶属于 StackOverflow