如何在骨干中加载预编译的模板

how to load precomplied template in backbone?

本文关键字:编译 加载      更新时间:2023-09-26

你能告诉我如何加载预编译的模板吗?我用谷歌搜索并找到解决方案.现在我不知道如何使用这个功能,你能告诉我如何使用这个功能吗?法典: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();
        };

我正在尝试加载目录内的html文件? template/test.html

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

    template: 'template/test.html',

});

您尝试使用的代码将替换木偶中的默认 HTML 机制。

  1. "template/test.html"将被翻译成"/app/templates/template/test.html.html",我想这不是你想要的(更改 url 生成或模板指针)
  2. 您的主干代码不假设"test.html"是在客户端上发生相反编译时预编译的,遵循 GET 响应,这是您想要的吗?
  3. 关于 Backbone 重写的使用,应该在尝试呈现 ToolItemView 之前调用它,所以基本上你可以在 ToolItemView 渲染之前的任何位置调用此代码。