backbonejs加载模型



在backbonej文档中,有人说我们可以在加载自举模型的情况下加载自举的模型

所以我和一样

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请求来从服务器获取这些数据

感谢

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

如何在Rails视图中安全地引导JSON提供了一种安全的方法(作者选择覆盖json_escape)。

您可能需要用一个单引号"'"替换所有&quot;,否则这是无效的javascript。

相关内容

  • 没有找到相关文章

最新更新