This project uses rails + backbone-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
$(document).ready ->
window.current_user = null
Notes.initialize()
2) to set up callback
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:
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
How should i use this global variable?
PS. response from server is correct.
Resolved in only by changing 1) to
$(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...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.