adamK
adamK

Reputation: 3889

Backbone render function: Unexpected token ILLEGAL

I have the following error when trying to render a backbone.js template:

Uncaught SyntaxError: Unexpected token ILLEGAL

From the following code, 2nd line, on the call to html:

render: function() {
        $(this.el).html(_.template(contactTemplate, {
            model: this.model.toJSON(),
        }));
        return this;
    }

I don't understand what the illegal character is or what is happening, any help would be much appreciated.

EDIT: Thank you for your help, you were correct, my template was at fault, turns out I had:

<p><a href="#profile/<%=model.accountId%">View</a></p>

Instead of

<p><a href="#profile/<%=model.accountId%>">View</a></p>

The joy of coding :)

Upvotes: 0

Views: 622

Answers (2)

JayC
JayC

Reputation: 7141

I think Backbone must have driven you a little object literal crazy!

render: function() {
        $(this.el).html(_.template(contactTemplate, {
            model: this.model.toJSON(),
        }));
        return this;
    }

Would only work (I think) if your template had fields specified like <%= model.field1 %>. Try this:

render: function() {
        $(this.el).html(_.template(contactTemplate, this.model.toJSON()));
        return this;
    }

Upvotes: 1

Givius
Givius

Reputation: 1008

you get that error when you are trying to access an undefined field from your model. by looking at your code your template has to look like this when you try to get the json values:

<b> the value of field AAA is <%= model.AAA %> </b>

if you wan to avoid to use model just call:

 _.template(contactTemplate, this.model.toJSON() )

then you can do something like

<b> the value of field AAA is <%= AAA %> </b>

Upvotes: 0

Related Questions