如何使用ember-cli和handlebars 2.0.0进行有条件的帮助? [英] How do I make a conditional helper with ember-cli and handlebars 2.0.0?

查看:108
本文介绍了如何使用ember-cli和handlebars 2.0.0进行有条件的帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {{#如果有权限my_permission }} 
//在这里做一些东西
{{else}}
// fallback
{{/ if}}

  {{#hasPermission session.user .permissionsmy_permission}} 

我无法想象这一点。我已阅读这个,但除了提供内容的帮助者之外,还没有太多解释或改变它例如{{rendersomething}}



当我尝试使其成为条件时,我得到:

  registerBoundHelper生成的助手不支持使用Handlebars块。 

非常感谢,谢谢!



编辑:



我的User对象被序列化:

  {用户:{
id:3,
name:john doe,
permissions:[
view_projects,
edit_milestones,
change_widgets
]}
}

我想能够在模板中检查某个权限,而不是将特定的权限绑定到计算属性。



PS Im使用ember 1.9.1,最新数据和handlebars 2.0

解决方案

根据 docs ,这是不可能的绑定帮助


绑定助手不支持使用Handlebars块或添加任何类型的子视图。


您可以可以将该逻辑推送到控制器,如下所示:

  App.IndexController = Ember.ArrayController.extend({
权限:my_permission,
hasPermission:function(){
var permission = this.get('permission');

返回权限==='my_permission';
} .property('permission')
});

然后,在您的模板中,您可以执行以下操作:

 < script type =text / x-handlebarsdata-template-name =index> 
{{#if hasPermission}}

< ul>
{{##each item in model}}
< li> {{item}}< / li>
{{/ each}}
< / ul>

{{else}}
// fallback
{{/ if}}
< / script>

工作解决方案这里


I am trying to achieve something similar to the form or function of:

{{#if has-permission "my_permission"}}
    // do some stuff here
{{else}}
    // fallback
{{/if}}

OR

{{#hasPermission session.user.permissions "my_permission"}}

I cannot figure this out. I've read this but there isn't much explaining on anything other than helpers that render content or alter it e.g. {{render "something"}}

When I try to make it a conditional I get this:

registerBoundHelper-generated helpers do not support use with Handlebars blocks.

Any help greatly appreciated, thanks!

EDIT:

My User object is serialized as such:

{"User": {
    "id": 3,
    "name": "john doe",
    "permissions": [
        "view_projects",
        "edit_milestones",
        "change_widgets"
    ]}
}

I want to be able to check a certain permission in the template, not bind a specific one to a computed property.

PS Im using ember 1.9.1, latest data and handlebars 2.0

解决方案

According to the docs, this is not possible with a bound helper

Bound helpers do not support use with Handlebars blocks or the addition of child views of any kind.

What you can do is push that logic to the controller as follows:

App.IndexController = Ember.ArrayController.extend({
  permission: "my_permission",
  hasPermission: function(){
    var permission = this.get('permission');

    return permission === 'my_permission';
  }.property('permission')
});

Then, in your template you can do:

<script type="text/x-handlebars" data-template-name="index">
  {{#if hasPermission }}

    <ul>
      {{#each item in model}}
        <li>{{item}}</li>
      {{/each}}
    </ul>

  {{ else }}
    // fallback
  {{/if}}
</script>

Working solution here

这篇关于如何使用ember-cli和handlebars 2.0.0进行有条件的帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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