javascript - 如何完成从下划线模板到 mustache 的For-Each循环

标签 javascript underscore.js mustache

我有一个下划线模板,我必须使用 Mustache来渲染它。下面是我的下划线模板:

<div id="sub-account">
    <p>something</p>
  <table>
    <tr><td>Name</td>

    </tr>
    <tbody>
        <% _.each(accountList, function(account) { %>
            <tr>
                <td><%= account.get('name') %></td>

            </tr>
        <% }) %>
    </tbody>
  </table>

 </div>

我使用 mustache 作为我的主要 View 来渲染列表。如何循环代码以呈现为 mustache 模板?

主干集合:

var SubAccountCollection = Backbone.Collection.extend({
    initialize: function(models, options) {
        this.account = options.id;
    },

    url: function() {
        return 'some/' +this.account + '/somelist';
    }
});

   return SubAccountCollection;
 });

这就是我试图用ajax调用做的事情:

accountList.fetch({

                success: function(accnlist){
                    setInterval(function(){
                        $('#sub-account-list').html(tmpl, {accnlist:accnlist.toJSON()})
                    }, 500);

                },
                error: function(err){
                    console.log('Error!', err)
                }
            });

最佳答案

应该是这样的。不过没查。

<div id="sub-account">
    <p>something</p>
  <table>
    <tr><td>Name</td>

    </tr>
    <tbody>
        {{#accountList}}
            <tr>
                <td>{{name}}</td>

            </tr>
        {{/accountList}}
    </tbody>
  </table>

 </div>

关于javascript - 如何完成从下划线模板到 mustache 的For-Each循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24132069/

相关文章:

javascript - 覆盖中断悬停事件

多个字段上的 JavaScript Backbone 集合排序

javascript - 使用 underscore.js 在深度嵌套的 JSON 中查找值

javascript - 将数字传递到 Handlebars.template.foo(bar)

javascript - Jquery On() 'click' 处理每件事?

javascript - 在 IE9 兼容性 View 中不显示 Font Awesome 图标

javascript - 通过Restangle中Spring POST响应的url位置 header 获取一个实体

javascript - 使用 Underscore JS 对 JSON 数组进行分组和聚合

elasticsearch - 如何从Elasticsearch中的过滤器获取所有文档?

javascript - 当鼠标悬停在县时,在工具提示中显示 CSV 中的关联指标