2013-03-27 22 views
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:服務器的響應是正確的。

回答

0

通過改變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() 

沒有找到還沒有任何其他辦法...