0

In backbonejs documentation it has been said that we can load bootstrapped models under loading bootstrapped models

So I did the same as there like

employeeCollection.reset(<%= @employees.to_json %>);

But I threw a error

Uncaught SyntaxError: Unexpected token &

When I clicked that error it took me to

employeeCollection.reset([{&quot;address&quot;:&quot;Madhyapur thimi 13, wamunae tole&quot;,&quot;fired&quot;:false,&quot;first_name&quot;:&quot;Gagan Shrestha&quot;,&quot;id&quot;:1,&quot;last_name&quot;:&quot;Shrestha&quot;,&quot;phone&quot;:&quot;9841971919&quot;},{&quot;address&quot;:&quot;Madhyapur thimi 13, wamunae tole&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;Prem Krishna&quot;,&quot;id&quot;:2,&quot;last_name&quot;:&quot;Shrestha&quot;,&quot;phone&quot;:&quot;9841755640&quot;},{&quot;address&quot;:&quot;sdgd&quot;,&quot;fired&quot;:false,&quot;first_name&quot;:&quot;fdsfs&quot;,&quot;id&quot;:3,&quot;last_name&quot;:&quot;sfdsf&quot;,&quot;phone&quot;:&quot;5345435&quot;},{&quot;address&quot;:&quot;sdfsf&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;dsfsd&quot;,&quot;id&quot;:4,&quot;last_name&quot;:&quot;fsd&quot;,&quot;phone&quot;:&quot;sfdsf&quot;},{&quot;address&quot;:&quot;Madhyapur thimi 13, wamunae tole&quot;,&quot;fired&quot;:false,&quot;first_name&quot;:&quot;Prafulla&quot;,&quot;id&quot;:5,&quot;last_name&quot;:&quot;Shrestha&quot;,&quot;phone&quot;:&quot;9841971919&quot;},{&quot;address&quot;:&quot;South africa&quot;,&quot;fired&quot;:false,&quot;first_name&quot;:&quot;ram&quot;,&quot;id&quot;:6,&quot;last_name&quot;:&quot;krishna&quot;,&quot;phone&quot;:&quot;86879979&quot;},{&quot;address&quot;:&quot;sdfdsf&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;fs&quot;,&quot;id&quot;:7,&quot;last_name&quot;:&quot;sdfdsfdsf&quot;,&quot;phone&quot;:&quot;sdfdsfds&quot;},{&quot;address&quot;:&quot;juj&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;rfr&quot;,&quot;id&quot;:8,&quot;last_name&quot;:&quot;hgtg&quot;,&quot;phone&quot;:&quot;9898&quot;},{&quot;address&quot;:&quot;gthy&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;olo&quot;,&quot;id&quot;:9,&quot;last_name&quot;:&quot;plo&quot;,&quot;phone&quot;:&quot;6766&quot;},{&quot;address&quot;:&quot;rfrf&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;uyuy&quot;,&quot;id&quot;:10,&quot;last_name&quot;:&quot;uyuy&quot;,&quot;phone&quot;:&quot;7676&quot;},{&quot;address&quot;:&quot;madhyapur thimi 12&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;shuvam1&quot;,&quot;id&quot;:11,&quot;last_name&quot;:&quot;shrestha&quot;,&quot;phone&quot;:&quot;1234556778&quot;},{&quot;address&quot;:&quot;dodale&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;sarita2&quot;,&quot;id&quot;:12,&quot;last_name&quot;:&quot;prajapati&quot;,&quot;phone&quot;:&quot;985757&quot;},{&quot;address&quot;:&quot;kathmandu&quot;,&quot;fired&quot;:false,&quot;first_name&quot;:&quot;hello&quot;,&quot;id&quot;:13,&quot;last_name&quot;:&quot;world&quot;,&quot;phone&quot;:&quot;0155675432&quot;}]);

I guess the error is due to escaping of json data, but not quite sure.

Do I need to do additional thing so that I can load necessary data when page loads, so that I need not fire ajax request to get those data from servers

Thanks

4

2 回答 2

1

Rails is HTML encoding your JSON. There are a few ways around this, but be aware that there are security implications (XSS) to just using something like html_safe.

How to Securely Bootstrap JSON in a Rails View gives a good overview of one way to do this safely (the author chooses to overwrite json_escape).

于 2013-02-15T02:22:51.550 回答
0

You probably need to replace all &quot; with a single quote "'", this is not valid javascript otherwise.

于 2013-02-14T02:09:59.103 回答