I would suggest that your problem is that you are storing #cart_template in the DOM:
<div id="cartlist"> <script type="text/template" id="cart_template"> </script> </div>
You create your CartList as follows:
var cart_view = new CartList({ el: $("#cartlist").html(myCart.showCart()) });
When you say this:
$("#cartlist").html(myCart.showCart())
everything that was inside #cartlist , in particular #cart_template . Then inside CartList you try to do this:
_.template( $("#cart_template").html(), {} );
But at this point there is more #cart_template , so $('#cart_template').html() is undefined and that where your error occurs: _.template will call replace internally, but you give it undefined as instead of a string.
The solution is to move your #cart_template outside of the #cartlist .
I would also recommend that you do not pass el the view constructor, your view should create its own el , and the caller should put that el where it wants it in the DOM. Thus, the view is fully responsible for its element, and you will have less problems with zombies and events.
source share