Reputation:
I have got my backbone model defined like this:
define (require) ->
Backbone = require 'backbone'
class IndexModel extends Backbone.Model
defaults:
status: ""
country: ""
language: ""
initialize: (attributes,options) ->
@set 'country', attributes.country
@set 'language', attributes.language ||= 'en'
url: -> "/v0/index/#{@get 'country'}/#{@get 'language'}.json"
And then my view like this:
define (require) ->
Backbone = require 'backbone'
template = require 'text!templates/index-page.html'
IndexModel = require 'cs!models/index'
class IndexView extends Backbone.View
template: _.template(template)
el: 'article.main'
events:
"click .image_button": "connect"
initialize: ->
_.bindAll(@, "render","connect")
@render()
render: ->
@$el.html(@template)
connect: (e) ->
@model = new IndexModel({country: e.currentTarget.alt, language: window.language})
@model.save()
console.dir @model
console.log 'Status: ', @model.get 'status
no
I am trying to get the status attributes but it appears to be empty and returning undefined.
What am i doing wrong here ?
Upvotes: 0
Views: 255
Reputation:
@robmisio thanks so much for your reply i tried both of your suggests but none worked for me.. i use this work around...
connect: (e) ->
@model = new IndexModel({country: e.currentTarget.alt, language: window.Language})
$('#spinner_ajax').show()
@model.save(null, {
success: @success
error: @error
})
error: (xhr, status, thrown) ->
console.log "AJAX Error: #{status}"
alert "AJAX Error: #{status}: Server is probably down."
$('#spinner_ajax').hide();
no
success: (model) ->
console.log "Status: #{model.get('status')}"
if model.get('status')
window.lmStatus = true
window.location.hash = 'connection'
$('#spinner_ajax p').html('You are being redirected ')
else
alert 'This connection is currently not active.'
$('#spinner_ajax').hide();
no
Upvotes: 0
Reputation: 1176
I'm assuming that the 'status' attribute is being set on the server. If so, the model.save() call is asynchronous and the attribute won't be available until it's complete. To access it, you would need to bind into the success callback passed when calling save, for example:
_self = @
@model.save success: ->
console.log 'Status: ', _self.model.get('status')
alternatively you could bind to the 'sync' event on the model which will fire after every successful save, for example:
@model.on 'sync', (model) ->
console.log 'Status: ', model.get('status')
Upvotes: 2