簡體   English   中英

如何在CoffeeScript的jQuery Ajax回調中設置window.variable

[英]How to set window.variable in jquery ajax callback in coffeescript

該項目使用rails + rails-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。 服務器的響應正確。

僅通過將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()  

找不到其他方法了...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM