1

I have a model whose data is displayed in a backbone view/underscore template.

I setup the template in my view like this:

return Backbone.View.extend({
        className: 'officeAlerts',
        template: _.template(OfficeAlertsTmpl, null, { variable: 'm' }),

And in my template, I have lines like this to display the model data:

<span class="textForEmployer">{%- m.officeName %} has no alerts.</span>

When all the data is there, everything works fine. The problem I have is with nulls. If a model attribute happens to be null, the whole page doesn't load and I get a null reference error in the browser console.

Is there a way to check/catch nulls so that it doesn't stop the whole page from loading?

Thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185

1 Answers1

1

You can simply add a condition like this:

<span class ="textForEmployer"> <%=  m ? m.officeName: "" %> has no alerts.</span>
T J
  • 42,762
  • 13
  • 83
  • 138