Last Updated: February 25, 2016
·
983
· kkemple

Async Loading of Templates in Backbone

window.loadTmpl = (function () {

    var templates = {}, temps, buildObj, getTemplate;

    $.get('templates/templates.html', function(html) {
        buildObj( html );
    });

    buildObj = function (html) {
        temps = $( html ).children('script');
        $.each( temps, function (index, template) {
            templates[template.id] = template.innerHTML.trim();
        });
    };

    getTemplate = function (temp) {
        return ( temp in templates ) ? templates[temp] : null;
    };

    return getTemplate;
}());