【问题标题】:How can accomplish For-Each loop from the underscore template to mustache如何完成从下划线模板到小胡子的 For-Each 循环
【发布时间】:2014-06-10 02:47:35
【问题描述】:

我有一个下划线模板,我必须使用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>

我使用小胡子作为我的主要视图来呈现列表。如何循环代码以呈现为小胡子模板?

主干集合:

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)
                }
            });

【问题讨论】:

  • 您使用的是 Underscore 模板还是 Mustache 模板?如果您使用 Mustache,那么 _.templateSettings 有什么关系?
  • 好吧,对。我不需要它。但我不确定如何使用 mustache 使用 for-each 循环给定上面的下划线函数
  • 所以您正在尝试在 Mustache 中实现您的 Underscore 模板,对吗? accountList 可能是一组 Backbone 模型吗? Mustache 真的更喜欢你给它一个简单的对象数组。
  • 您能否举个例子来说明您要提出的建议。谢谢
  • 我仍然不知道accountList 是什么或它来自哪里。它起源于 Backbone 集合吗?如果是,那么toJSON 可能会有所帮助。

标签: javascript underscore.js mustache


【解决方案1】:

应该是这样的。但没有检查。

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

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

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

 </div>

【讨论】:

猜你喜欢
  • 2015-04-10
  • 1970-01-01
  • 2012-08-20
  • 2013-01-18
  • 2014-03-29
  • 2013-08-12
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
相关资源
最近更新 更多