backbone.js - Backbonejs -collection is not defined -
i'm getting collection not defined error in backbone.
var categorylist = backbone.view.extend({ el:'#content', render: function() { var = 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); } }) } });
and in script i'm running each loop add data table, i'm getting 'cats not defined error'.
<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>
you have transform collection plain javascript object using tojson
method.
var template = _.template($('#category-list-template').text()); var result = template({collection: cats.tojson()}); that.$el.html(result);
Comments
Post a Comment