fevangelou
9/9/2012 - 7:41 AM

Load underscore templates with jQuery $ajax()

Load underscore templates with jQuery $ajax()

window.renderTemplate = function(name, el, json)
{
	var url = '/templates/' + name + '.html';
	$.ajax({
		url: url,
		method: 'GET',
		async: false,
		dataType: 'html',
		success: function(data) {
			var tmpl = _.template(data);
			$(el).html(tmpl(json));
		}
	});
}

// Implementing it in the backbone.

window.MyView = Backbone.View.extend({
	el: $('#mainframe'),
	render: function(eventName) {
		renderTemplate('tmpl_name', this.el, {description: 'This is first template'});
		return this;
    }
});