【问题标题】:Parameters of a custom ember block helper自定义 ember 块助手的参数
【发布时间】:2014-01-08 05:35:46
【问题描述】:

我正在编写一个简单的餐厅菜单,我需要检查把手模板内的两个值是否相等。更具体地说,我有一堆 dish 模型,它们有一个 category_id 字段。所以我需要显示一个dish,如果它与我们现在所在的类别具有相同的category_id

我有以下帮手:

Em.Handlebars.registerHelper 'ifEqual', (v1, v2) -> 
  # debugging
  console.log v1, v2
  return v1 == v2

我是这样使用的:

.categories
  =hb 'each c in categories' do
    %ul
      %li=hb 'c.name'
        =hb 'each d in dishes' do
          %ul
            =hb 'ifEqual d.category_id c.id' do
              %li=hb 'd.name'

(对不起,hamlbars 和 coffeescript,但应该很清楚)

问题是console.log 输出字符串"d.category_id""c.id",我不知道为什么会这样。

另一方面,非块帮助器工作得很好,并且输出实际值

Em.Handlebars.helper 'console-log', (v1, v2) -> 
  console.log typeof(v1), v1
  console.log typeof(v2), v2

任何想法为什么它会以这种方式工作以及我做错了什么?

【问题讨论】:

    标签: ember.js coffeescript haml


    【解决方案1】:

    只是为了节省您的时间,这在 ember 车把中不起作用,您需要将逻辑添加到控制器并仅使用 if/unless。

    Is it possible to pass conditionals or other javascript as arguments in ember handlebars?

    【讨论】:

    • 嗯...我不知道如何让控制器知道我们现在拥有哪个类别的id。如何访问它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 2014-10-05
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    相关资源
    最近更新 更多