0
该项目使用rails + backbone-on-rails。 我试过这个& ......但我仍然无法在响应回调中设置全局变量。这里就是我试图做的:可变的
1)初始化在同一个地方骨干初始化如何在coffeescript中设置window.variable jquery ajax回调
$(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 [email protected]('password') && [email protected]('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:服务器的响应是正确的。