//path = the location of the external file
//scriptBlockId = the id of the external script block (<script id="add-form-tempate" type="text/html-template">)
//fillId = where you want to place the template when rendered
var App = Backbone.View.extend({
render: function (path, scriptBlockId, fillId) {
$.ajax({
async: false,
dataType: 'html',
method: 'GET',
url: path,
success: function (response) {
//Not sure why we have to do this first, before we can select the script block?
var section = $('#main').append(response);
var templateString = $(section).find('#' + scriptBlockId).html();
var compiledTemplate = _.template(templateString);
var temp = compiledTemplate();
$(fillId).html(temp);
}
});
}
});
var app = new App();
app.render(window.siteRoot + 'Scripts/_test1.tmpl.html', 'add-form-template', '#main');
此代碼有效!爲什麼我們必須先追加我不知道的......使用Underscore.js加載外部模板
通知異步:假 – Mark