Pascal
Pascal

Reputation: 2405

backbone previous attribute showing in get

A simple backbone save resulting in a fail get showing previous attribute.

I have an attribute captcharesponse which states if the captcha is ok while saving.

The attribute is well sync and showing in attribute when i make a console model.

But when i make a model.get(captcharesponse) ; i always get the previous attribute.

Here is my code.

var set = { 
    'nom' : v.ui.nom.val()
    , 'mail' : v.ui.mail.val()
    , 'suject' : v.ui.suject.val()
    , 'telephone': v.ui.telephone.val()
    , 'message': v.ui.message.val()
    , 'recaptcha_response_field': v.ui.recaptcha_response_field.val() 
    , 'challenge' : RecaptchaState.challenge 
} ;                 
model.set( set ) ;
model.save( { wait : true }) ;
console.log ( model ) ; // new sync attributes in 
var captcharesponse = model.get( "captcharesponse" ) ;

Please help

Upvotes: 1

Views: 104

Answers (1)

Sushanth --
Sushanth --

Reputation: 55750

Save is asynchronous.

So model.get( is executed even before the response is served back from the server. use a callback instead..

Listen to the sync event and log your response there..

// set variable
// Listen to the model sync event
// this keyword relates to the view if you have any
this.listenTo(model, 'sync', 'getCapcha');
model.set( set ) ;
model.save( { wait : true }) ;

function getCapcha(responseModel) {
    console.log ( responseModel) ; // new sync attributes in 
    var captcharesponse = responseModel.get( "captcharesponse" ) ;
}

Upvotes: 2

Related Questions