[英]How to set window.variable in jquery ajax callback in coffeescript
This project uses rails + backbone-on-rails. 该项目使用rails + rails-on-rails。 I've tries this & that... but still i can't set global variable in a response callback.
我已经尝试过这个&那...但仍然无法在响应回调中设置全局变量。 here's what i tried to do:
这是我尝试做的事情:
1) Initialization of variable in the same place backbone is initialized 1)初始化同一位置主干中的变量
$(document).ready ->
window.current_user = null
Notes.initialize()
2) to set up callback 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) and finally execute this method: 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!')
output of console.warn window.current_user
is null
console.warn window.current_user
输出为null
How should i use this global variable? 我应该如何使用此全局变量?
PS. PS。 response from server is correct.
服务器的响应正确。
Resolved in only by changing 1) to 仅通过将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()
Didn't find any other way yet... 找不到其他方法了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.