简体   繁体   English

将动作名称传递给应该在助手输出中的Handlebars助手

[英]Pass action name to Handlebars helper that should be in helper output

I am trying to create a FontAwesome handlebars helper that can also add an action to the html output. 我正在尝试创建一个FontAwesome把手帮助器,也可以将操作添加到html输出中。

The handlebars helper takes color and action as optional parameters: 车把帮手将coloraction作为可选参数:

Em.Handlebars.helper('icon', function (iconName, options) {
    var returnString = '';

    options = options.hash;

    returnString += "<i class='icon-" + Handlebars.Utils.escapeExpression(iconName) + "'";
    returnString += options.color ? " style='color: " + options.color + "'" : "";
    returnString += options.action ? " {{action '" + options.action + "'}}" : "";
    returnString += "></i>";

    return new Handlebars.SafeString(returnString);
});

And here is how I am using it: 这是我的使用方式:

{{icon 'angle-right' action='expand'}}

Action in controller looks like this: 控制器中的动作如下所示:

actions: {
    expand: function() {
        console.log('expanded!');
    }
}

But it doesn't work... When I click the icon, nothing is logged. 但这不起作用...当我单击该图标时,没有任何记录。 Is it not possible for Handlebars helpers to output Handlebars code this way? 手把助手不能以这种方式输出手把代码吗? Is there another way of adding an action to the html output element? 还有另一种向html输出元素添加操作的方法吗?

Something like this is where you'd want to use a component instead of a Handlebars helper. 这样的事情就是您要使用组件而不是Handlebars帮助器的地方。 (Handlebars helpers are very limited in what they can do.) (车把帮手的工作能力非常有限。)

App.FontAwesomeIconComponent = Em.Component.extend({
    color: '',
    styleAttribute: function() {
        return 'color:' + this.get('color') + ';';
    }.property('color'),

    icon: '',
    fullIconName: function() {
        return 'icon-' + this.get('icon');
    }.property('icon')
});

Then, for your template: 然后,为您的模板:

<i {{bind-attr class='fullIconName' style=styleAttribute}}
    {{action 'iconClicked' on='click'}}>
</i>

Then, you can use it like so: 然后,您可以像这样使用它:

{{font-awesome-icon icon='angle-right' color='blue' iconClicked=expand}}

This will then call the expand action in your controller when the icon is clicked. 单击该图标后,这将在控制器中调用expand操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM