Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在coffeescript中的jquery ajax回调中设置window.variable_Javascript_Jquery_Ajax_Backbone.js_Coffeescript - Fatal编程技术网

Javascript 如何在coffeescript中的jquery ajax回调中设置window.variable

Javascript 如何在coffeescript中的jquery ajax回调中设置window.variable,javascript,jquery,ajax,backbone.js,coffeescript,Javascript,Jquery,Ajax,Backbone.js,Coffeescript,此项目使用rails+rails上的主干。 我试过这个和那个。。。但我仍然不能在响应回调中设置全局变量。以下是我试图做的: 1) 在初始化主干的同一位置初始化变量 $(document).ready -> window.current_user = null Notes.initialize() 2) 设置回调 authorize: -> jQuery.ajax( type: 'POST' url: '/api/sessions.json

此项目使用rails+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窗口的输出。当前用户
null

如何使用此全局变量?

另外,服务器的响应是正确的。

仅通过将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()