从另一个助手调用一个handlebars块助手 [英] Calling a handlebars block helper from another helper

查看:127
本文介绍了从另一个助手调用一个handlebars块助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从另一个帮助器中调用一个handlebars块助手。

How can I call a handlebars block helper from another helper in ember.

我有兴趣在绑定的帮助器中转换以下内容。

I am interested in converting the following in a bound helper.

    {{#each link in sideMenuLinks}}
        <li class="navigation page">
            {{#linkTo ROUTE_VARIABLE link.linkToRouteContext}}
                {{{link.iconTag}}}<i class="icon-right-open"></i>{{link.linkText}}</a>
            {{/linkTo}}
        </li>
    {{/each}}

请注意,我必须调用{{# linkTo}}阻止助手,并且还可以通过属性link.linkToRoute中的值更改ROUTE_VARIABLE。

Note that I will have to call a {{#linkTo}} block inside the helper and also change ROUTE_VARIABLE with a value from the property link.linkToRoute.

推荐答案

Ember手柄帮助放在 Ember.Handlebars.helpers 。您可以使用 Ember.Handlebars.helpers。{helperName} .call 来调用它们。

The Ember handlebars helpers are placed at Ember.Handlebars.helpers. You can call them with Ember.Handlebars.helpers.{helperName}.call.

然而,以上的外观像一个动态的部分/视图样式助手。我建议为它创建一个Handlebars View帮手。语法相似,但您将View类传递给帮助器

However, The above looks like a dynamic partial/view style helper. I would suggest creating a Handlebars View helper for it. The syntax is similar but you pass in a View class to the helper.

  Ember.Handlebars.helper('sideMenuLinks', App.SideMenuLinksView);

相应的视图可以使用与您的模板类似的模板,方法是提供一个 templateName

The corresponding view can use a template similar to yours by giving a templateName

  App.SideMenuLinksView = Ember.View.extend({
    templateName: 'sideMenuLinksTemplate'
  });

你的模板将是这样的,

 <script type='text/x-handlebars' data-template-name='sideMenuLinksTemplate'>
   {{#each link in view.links}}
       <li class="navigation page">
           {{#linkFor parentView.routeVariable link.linkToRouteContext}}
               {{{link.iconTag}}}<i class="icon-right-open"></i>{{link.linkText}}</a>
           {{/linkFor}}
       </li>
   {{/each}}    
 </script>

默认Ember linkTo 是静态的您不能从变量传递命名路由。您将需要一个动态的 linkTo 帮助器,像 linkFor
之前查找变量路径应用 linkTo 内部

The default Ember linkTo is static, In that you cannot pass the named route from a variable. You will need a dynamic linkTo helper like the linkFor that looks up the variable path before applying linkTo internally.

  Ember.Handlebars.registerHelper('linkFor', function(path, params, options) {
    var view = options.data.view;
    var name = view.get(path);
    var args = [name, params, options];

    return Ember.Handlebars.helpers.linkTo.apply(this, args);
  });

最后你可以使用这个帮助器,如下所示。 链接属性将绑定到此示例中控制器的内容

Finally you can use this helper like below. The links property will be bound to the content of the controller in this example.

 <script type='text/x-handlebars' data-template-name='application'>
   {{sideMenuLinks links=content routeVariable='page'}}
 </script>

这是一个工作 jsbin

这篇关于从另一个助手调用一个handlebars块助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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