Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template error upgrading to underscore 1.7

When upgrading my web app from underscore 1.6 to 1.7, I receive the following error "list is not defined". When using underscore 1.6 it works perfectly. Any ideas?

//acquire the list template
$.get('tpl/listTpl.html', function(templates) {

//run underscore js on the list template and pass in the full collection of models
var template = _.template(templates, {list:app.collections.list.models});

//load the underscore template into the DOM
that.$el.html(template);

});

like image 655
Stuart Semple Avatar asked Aug 29 '14 14:08

Stuart Semple


People also ask

What is underscore template?

The _. template() function is an inbuilt function in the Underscore. js library of JavaScript which is used to compile JavaScript templates into functions that can be evaluated for rendering.

How do you use underscore in JavaScript?

Adding Underscore to a Node. Once added, underscore can be referred in any of the Node. js modules using the CommonJS syntax: var _ = require('underscore'); Now we can use the object underscore (_) to operate on objects, arrays and functions.

What does underscore mean in JavaScript?

The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.


1 Answers

From the 1.7.0 changelog:

Underscore templates no longer accept an initial data object. _.template always returns a function now.

You will need to change your code to the following:

$.get('tpl/listTpl.html', function(templates) {
  var template = _.template(templates);
  var result = template({list:app.collections.list.models});
  that.$el.html(result);
});
like image 118
Ishmael Smyrnow Avatar answered Oct 21 '22 14:10

Ishmael Smyrnow