我有一个应用程序,可以通过 Devise 管理 Rails 的注册/输入/退出。
当我登录时,我被重定向到 Backbone 启动的 Dashboard#index。
我想以某种方式在 Backbone 中检索我的 current_user.id 和 current_user.token 。
我唯一的想法是将其放在我的dashboard.html.haml中
:javascript
window.App.current_user.id = "#{current_user.id}"
window.App.current_user.token = "#{current_user.token}"
(请注意,客户端只能访问公共信息)
对于更优雅的解决方案,您可以创建一个UserSession
骨干模型和fetch
它从服务器作为正常的骨干模型。
window.App.UserSession = Backbone.Model.extend({
urlRoot: "/session"
});
如果不想提出此额外请求,您可以使用模板渲染时已有的数据显式加载模型:
window.App.current_user =
new window.App.UserSession({
id: "#{current_user.id}",
token: "#{current_user.token}"
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)