backbonejs加载模型

时间:2013-02-14 02:06:56

标签: backbone.js loading models

在backbonejs文档中,我们可以说我们可以在loading bootstrapped models下加载自举模型

所以我做了同样的事情

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 个答案:

答案 0 :(得分:1)

Rails是对您的JSON进行HTML编码。有几种方法可以解决这个问题,但要注意使用像html_safe这样的东西会产生安全隐患(XSS)。

How to Securely Bootstrap JSON in a Rails View很好地概述了安全执行此操作的一种方法(作者选择覆盖json_escape)。

答案 1 :(得分:0)

您可能需要使用单引号&quot;替换所有"'",否则这不是有效的JavaScript。