2013-02-14 67 views
0

在backbonejs文件已經說我們可以在loading bootstrapped modelsbackbonejs加載模型

加載自舉模式所以,我也一樣,因爲喜歡

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

但我扔了一個錯誤

Uncaught SyntaxError: Unexpected token & 

當我點擊那個錯誤,它把我帶到

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;}]); 

我想這個錯誤是由於轉義JSON數據,但不是很確定。

我需要做更多的事情,所以,當頁面加載,所以我不必火Ajax請求從服務器獲取這些數據

感謝

+2

這可能會有幫助:[如何在Rails視圖中安全地引導JSON](http://jfire.io/blog/2012/04/30/how-to-securely-bootstrap-json-in-a- rails-view /) – steveax 2013-02-14 02:18:06

+1

您的JSON正在獲取HTML編碼,請參閱@steveax的解決方案鏈接。 – 2013-02-14 02:57:30

+0

@steveax你必須寫在回答部分,以便我可以接受你的答案:) – Gagan 2013-02-15 02:17:07

回答

1

Rails是HTML編碼您的JSON。有幾種方法可以解決這個問題,但請注意,只有使用諸如html_safe之類的東西纔會產生安全隱患(XSS)。

How to Securely Bootstrap JSON in a Rails View給出了一種安全地做到這一點的好方法(作者選擇覆蓋json_escape)。

0

你可能需要更換我可以加載必要的數據所有&quot;用單引號"'",否則這是無效的javascript。