Friday, 15 July 2011

javascript - UnderscoreJS Uncaught TypeError: Cannot call method 'replace' of undefined -


I have my backbone view:

Note: Template: _Template ($ ('# Note-Template'). HTML ()),

The template that is throwing this error is:

  & lt; Script type = "text / template" id = "note-template" & gt; & Lt; Div class = "reminder" & gt; & Lt; Div class = "reminder-hover" & gt; & Lt; Div class = "title" & gt; & Lt;% = Title% & gt; & Lt; / Div & gt; & Lt; Div class = "created" & gt; Created 3 days ago & lt; Span class = "pull-right" & gt; In 3 hours & lt; / Span & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Span class = "id" style = "display: none;" & Gt; & Lt;% = id% & gt; & Lt; / Span & gt; & Lt; / Div & gt; & Lt; / Script & gt;   

I'm confused because it works in my console:

& gt; & Gt; _.template ($ ('$ note-template') .HTML ());
function (n) {return e.call (this, n, w)}

Here's the full code:

  App.Views.Index = Backbone.View.extend ({el: $ ("div.reminders"), today's: $ ("Span.today"), Tomorrow: $ ("span.tomorrow"), week : $ ("Span.week"), all_times: $ ("span.all-time"), note: template: _.template ($ ('# -template') .HTML ()), Event: {" Click "NewNote", start: function () {_.bindAll (this, 'render'), this .notes = this.options.notes; this .listenTo (this.model, 'change', this.render);}, render: function () {// Hide notes related to editing a note Val (""); $ ("[name = title]") Hide (). Val (""); $ ($) "Save1")Wrap (), it $ ("Close1"). Hide (); // Fill the content. $ El.html (this.noteTemplate (this.model.toJSON ())); $ Todays.html (collection.getTodays.length); this. $ Tomorrows.html (collection.getTomorrows.length); this. $ Weeks.html (collection .getWeeks.length); this. $ All_times.html (collection.getAllTimes.length); This return; }, EditNote: function () {this.goTo ("Notes /" + this.model.id); }, New note: function () {this.goTo ("new note"); }});    

When you define the method, instead of trying to cache the HTML template Do this when you initialize the scene. initialize: function () {// ... this.nodeTemplate = _.template ($ ('# -template') .html ()); }

It is highly likely that you are defining the view before the DOM (and thus the template) is loading.

No comments:

Post a Comment