how to load precomplied template in backbone?

2019-03-06 04:12发布

could you please tell me how to load precomplied templates .I googled it and find a solution .Now I don't know how to use this function.could you please tell me how to use this function ? code: http://goo.gl/ALfkzf

Backbone.Marionette.TemplateCache.prototype.loadTemplate = function (templateId, callback) {
            var tmpId = templateId.replace("#", ""),
                    url = "/app/templates/" + tmpId + ".html";

            $.get(url, function (templateHtml) {
                compiledTemplate = Handlebars.compile($(templateHtml).html())
                callback.call(this, compiledTemplate);
            });
        };

        Backbone.Marionette.Renderer.renderTemplate = function (templateId, data) {
            var renderer = $.Deferred();
            Backbone.Marionette.TemplateCache.get(templateId, function(template){
                var html = template(data);
                renderer.resolve(html);
            });
            return renderer.promise();
        };

I am trying to load html file which is inside the directory ? template/test.html

var ToolItemView = Backbone.Marionette.ItemView.extend({

    template: 'template/test.html',



});

1条回答
倾城 Initia
2楼-- · 2019-03-06 04:57

The code that you are trying to use, replaces default HTML mechanism in Marionette.

  1. 'template/test.html' will be translated to "/app/templates/template/test.html.html", which I guess is not what you want (Either change url generation, or template pointer)
  2. Your Backbone code, does not assume, that "test.html" was precompiled on contrary compilation is happening on the client, following GET response, is this what you want?
  3. Regarding Backbone override use, it should be called before you try to render your ToolItemView, so basically you can call this code anywhere before ToolItemView render.
查看更多
登录 后发表回答