【问题标题】:Pass specific converters/templates to template with jsViews/jsRender使用 jsViews/jsRender 将特定转换器/模板传递给模板
【发布时间】:2013-10-08 10:21:53
【问题描述】:

我正在尝试仅将转换器和/或模板传递给特定模板。根据the API,您只能传入 helpers,而不能传入转换器或模板。

有没有办法做到这一点,或者有人知道将来是否计划支持这个?

注意通过$.views.templates({...})$.views.converters({...}) 全局传递它们并不是一个真正的选择,因为我将有办法使用许多甚至可能有名称冲突的模板和转换器。

【问题讨论】:

    标签: templates jsrender jsviews


    【解决方案1】:

    您可以使用您的模板声明转换器 - 它们将是模板私有的。见Registering templates: $.templates()。查找“高级场景:将私有资源与模板关联”

    此外,the API for registering converters:$.views.converters({...}) 还允许您在全局范围内或仅为特定模板在本地注册一个转换器(或一组转换器)。请参阅“将转换器添加为父模板的私有资源”部分。要使它们成为模板的本地或私有,只需在您的 converters() 调用中将模板作为最后一个参数传递。

    所以这里是一个带有自己特殊转换器的模板:

    $.templates({
      myTemplate: {
        markup: "Use my converter {{myconv:name}}",
        converters: {
          myconv: function(val) { return myCalculatedValue; }
        }
      }
    });
    

    现在{{myconv:...}} 专用于myTemplate,不会在其他地方提供。

    现在假设我想动态替换“myconv”,仍然在myTemplate 内。我可以随时使用converters() API 添加/更改它:

    $.views.converters(
      "myconv",
      function(val) { return myNewUpdatedCalculatedValue; },
      $.templates.myTemplate // Only override it for myTemplate, not globally...
    );
    

    以下是一些相关链接:

    【讨论】:

    • 太棒了!我在看完全错误的位置,我认为它类似于 link(selector, helpers)render(helpers) 样式中的 Helpers。也许您可以在文档中使这一点更加明显?还是非常感谢!
    • @mrs_sheep:本地转换器现在会覆盖同名的全局转换器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 2019-02-15
    • 1970-01-01
    相关资源
    最近更新 更多