【问题标题】:how to load precomplied template in backbone?如何在主干中加载预编译模板?
【发布时间】:2015-01-20 05:36:32
【问题描述】:

能否请您告诉我如何加载预编译的模板。我搜索了它并找到了解决方案。现在我不知道如何使用此功能。请您告诉我如何使用此功能? 代码: 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 文件?模板/test.html

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

    template: 'template/test.html',



});

【问题讨论】:

  • 使用这个我将创建一个 Handlebars.js 的依赖项我可以以另一种方式加载模板

标签: javascript jquery backbone.js marionette backbone-views


【解决方案1】:

您尝试使用的代码替换了 Marionette 中的默认 HTML 机制。

  1. 'template/test.html'将被翻译成“/app/templates/template/test.html.html”,我猜这不是你想要的(要么改变url生成,要么模板指针)
  2. 您的 Backbone 代码不假定“test.html”已被预编译,相反,客户端上正在发生编译,在 GET 响应之后,这是您想要的吗?
  3. 关于 Backbone 覆盖的使用,它应该在你尝试渲染你的 ToolItemView 之前被调用,所以基本上你可以在 ToolItemView 渲染之前的任何地方调用这个代码。

【讨论】:

  • 等我做点什么,请您更改该代码
  • goo.gl/LGUEBv这个我做更多。我没有收到任何错误仍然页面没有加载
  • 你知道如何使用 Firebug 或 Chrome 开发工具吗?
  • 您提供的链接显示,仅部分代码。我看到“Uncaught ReferenceError: render is not defined”,我建议你在写任何东西或提问之前学习使用开发工具。
  • sorry sir ...goo.gl/OrGjRZ ..实际上我已经定义了渲染函数..我知道我遇到了错误,但是我在脚本标签上定义了渲染函数
猜你喜欢
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
相关资源
最近更新 更多