user2942566
user2942566

Reputation: 455

How can accomplish For-Each loop from the underscore template to mustache

I have an underscore template and I have to use Mustache to render it. Below is my underscore template:

<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>

Im using a mustache as my main view to render a list. How can I loop through the code to render as a mustache template?

Backbone Collection:

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

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

   return SubAccountCollection;
 });

This is what im trying to do with the ajax call:

accountList.fetch({

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

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

Upvotes: 1

Views: 425

Answers (1)

Nik Terentyev
Nik Terentyev

Reputation: 2310

should be something like that. didn't check though.

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

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

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

 </div>

Upvotes: 1

Related Questions