Underscore.js Template Issue - Não é possível chamar o método 'replace' de null
Eu estive olhando e encontrei muitas respostas, mas nenhuma parece funcionar.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Shopping Cart
</title>
<link rel="stylesheet" href="lib/style.css" type="text/css">
</head>
<body>
<script id="rtemp" type="text/x-underscore-template"">
<span><%= title %></span>
</script>
<script src="lib/jquery.js" type="text/javascript"></script>
<script src="lib/underscore.js" type="text/javascript"></script>
<script src="lib/backbone.js" type="text/javascript"></script>
<script src="lib/script.js" type="text/javascript"></script>
</body>
<script>
var Photo = Backbone.Model.extend({
initialize: function(){
console.log('this model has been initialized');
this.bind("change:title", function(){
var title = this.get("title");
console.log("My title has been changed to.. " + title);
var pv = new PhotoView();
pv.render();
});
},
setTitle: function(newTitle){
this.set({ title: newTitle });
},
setLocation: function(newLoc)
{
this.set({location:newLoc});
}
});
var PhotoView = Backbone.View.extend
({
el: $('body'),
render: function(event)
{
var name = myPhoto.get('title');
console.info(name);
var template = _.template($('#rtemp').html(), {title:name});
console.info(this.model);
$(this.el).html(template);
return this;
}
});
</script>
</html>
Primeiro;
Crie uma nova instância do método
var newPhoto = new Photo();
newPhoto.setTitle('Fishing');
Este trabalho funciona bem, ele será carregado no corpo através do modelo. No entanto, se eu fizer isso de novo,
newPhoto.setTitle('Sailing');
Eu recebo o erro - "Não é possível chamar o método 'substituir' do nulo"
Não há erro de linha, mas acredito que está em
var template = _.template($('#rtemp').html(), {title:name});