2016-01-07 3 views
0

Я получаю коллекцию, не определенную ошибку в позвоночнике.Backbonejs -collection не определен

var CategoryList = Backbone.View.extend({ 
    el:'#content', 
    render: function() { 
     var that = this; 
     var cats = new Categories(); 
     cats.fetch({ 
      success: function(cats) { 
       var template = _.template($('#category-list-template').html(), {cats: cats.models}); 
       cats.each(function(it) { 
        console.log(it.toJSON()); 
       }); 
       that.$el.html(template); 
      } 
     }) 
    } 
}); 

И в этом сценарии я бег в каждый цикл, чтобы добавить данные в таблицу, но я получаю "кошка не определена ошибки.

<script type="text/template" id="category-list-template"> 

      <table class="table striped"> 
      <thead> 
      <tr> 
      <td>Id</td> 
      <td>Name</td> 
      </tr> 
      </thead> 
      <tbody> 
      <% _.each(cats, function(category) { %> 
      <tr> 
      <td><%= category.name %> </td> 
      </tr> 
      <% }); %> 
      </tbody> 

      </table> 

    </script> 

ответ

0

Вам необходимо преобразовать коллекцию в простой объект javascript, используя метод toJSON.

var template = _.template($('#category-list-template').text()); 
var result = template({collection: cats.toJSON()}); 
that.$el.html(result);