S
S
semolex2014-04-18 10:30:28
backbone.js
semolex, 2014-04-18 10:30:28

Underscore template variables do not accept values ​​from the fetch() method.(Uncaught ReferenceError: variable is not defined )

Hello!
Such is the problem - there is a data set in the database. When I "take out" it with the fetch() method, the data comes. But it is not possible to transfer to a variable on the Underscore-temple ...
Can anyone tell me what is the reason? Thank you!
Here is the code for a better explanation :)

var AdminView = Backbone.View.extend({

el: "#content",
render: function() {

    var that = this;
    var template = _.template($('#adminpage').html()); //it works and render main template
    that.$el.html(template);
    var test = new UsersCollection();

    test.fetch({
        success: function(data) {
            console.log(test.toJSON());//it works too,showing data in console
            var template = _.template($('#users_table').html(), {
            test: test
            });
            that.$el.html(template);
        }
    });

And accordingly the HTML template
<% _.each(test,function(user,key,list){ %>
 <tr>
   <td><%= key %></td>

   <td><%- user.l_name %></td>

   <td><%- user.f_name %></td>

   <td><%- user.login %></td>

   <td><%- user.email %></td>

   <td><%- user.id_role %></td>

   <td>
     <div class="small success btn">


 </tr>
 <% }) %>*/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2014-04-18
@semolex

You are passing a collection to the template, and the template doesn't expect it at all.

var template = _.template($('#users_table').html(), {
            test: test.toJSON()
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question