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:服務器的響應是正確的。