此项目使用rails + backbone-on-rails。
我试过这个&那...但我仍然不能在响应回调中设置全局变量。这是我试图做的事情:
1)初始化相同位置骨架中变量的初始化
$(document).ready ->
window.current_user = null
Notes.initialize()
2)设置回调
authorize: ->
jQuery.ajax(
type: 'POST'
url: '/api/sessions.json'
wait: true
data:
email: @get('email')
password: @get('password')
).success( (response) ->
exports = this
exports.current_user = response
window.current_user = response
`window.current_user = response`
3)并最终执行此方法:
loginUser: (e)->
e.preventDefault()
if !@validateField('password') && !@validateField('email')
return false
@model.attributes = @readAttributes()
@$('#errors').text()
@model.authorize() # call a method defined above
console.warn window.current_user
if window.current_user
@$('#errors').text('You\'ve successfuly logged in ' )
else
@$('#errors').text('Wrong email/password!')
console.warn window.current_user
的输出为null
我该如何使用这个全局变量?
PS。来自服务器的响应是正确的。
答案 0 :(得分:0)
仅通过将1)更改为
来解决$(document).ready ->
window.current_user =
name: ""
company: ""
created_at: ""
email: ""
facebook: ""
id: ""
license_id: ""
linkedin: ""
password_digest: ""
phone: ""
title: ""
twitter: ""
updated_at: ""
web: ""
Notes.initialize()
还没有找到任何其他方式...