外部车把模板骨干木偶 [英] External handlebars templates backbone marionette

查看:43
本文介绍了外部车把模板骨干木偶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我添加了Marionette.sync插件并覆盖了以下方法:

In my application i added Marionette.sync plugin and override these methods:

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 (template, data) {
    template(data);
};

但这行不通,有什么主意吗?

But this not work, any ideas?

推荐答案

由于您提到Marionette.Async插件,因此我假设您正在运行Marionette v0.9.

I assume you're running v0.9 of Marionette since you mention the Marionette.Async plugin.

方法名称中的Renderer更改略有改变,并且不再调用TemplateCache对象.

The Renderer change is slightly off in the method name, and nothing is calling your TemplateCache object anymore.

如果您打算使用预编译的把手功能,则只需执行以下操作:

If you're intending to use pre-compiled Handlebars functions, then you only need to do this:


Backbone.Marionette.Renderer.render = function(template, data){
  return template(data);
};

如果您打算使用TemplateLoader异步加载模板然后进行编译,则您的代码将需要如下所示:

If you intend to have the template loaded asynchronously and then compiled, using the TemplateLoader, your code would need to look like this:


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();
};

渲染器负责调用TemplateCache.

The Renderer is responsible for calling the TemplateCache.

旁注:您使用什么文章/博客文章/wiki页面/文档来获取代码?我可能错过了一些需要更新的页面.

Side note: what article / blog post / wiki page / documentation were you using to get your code from? I have probably missed some pages that need to be updated.

这篇关于外部车把模板骨干木偶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆