【问题标题】:how to check a model property using if conditions in backbone template如何使用主干模板中的 if 条件检查模型属性
【发布时间】:2016-02-10 23:10:45
【问题描述】:

我正在尝试使用主干模板来呈现带有模型对象的表格。在渲染表格时,我必须根据模型属性创建一个表格列。我试着做

<script id="table-rows" type="text/template">
<td> <span class="status"> {{ status }} </span> </td>
<% if (status == 'completed') { %>
<td>
something
</td>
<% } %>
</script>

我的单行视图正在调用渲染方法,如

render: function () {
this.$el.html(this.template(this.model.toJSON()));
return this;
}

但即使条件不匹配,这也会始终呈现列。 让我知道哪里出错了。 谢谢

【问题讨论】:

  • 你的_.templateSettings是什么样的?
  • _.templateSettings.interpolate = /\{\{(.+?)\}\}/g;

标签: backbone.js underscore.js


【解决方案1】:

为什么不试试这个:

<% if ('{{status}}' == 'completed') { %>
<td>
something
</td>
<%}%>

【讨论】:

  • 不走运,条件总是通过并打印表格单元
  • 你能在 td 中输出 status 的值,以便我们确保它没有“完成”吗?
  • {{ status }} 值打印正确,就像行的运行和完成一样。但 if 条件不起作用
  • 也没有评估它只是省略这个块并打印表格单元格。
  • 你能提交更多的代码吗?一定有其他事情发生。
猜你喜欢
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 2013-11-27
  • 1970-01-01
相关资源
最近更新 更多