javascript - jquery (a)synchronous request to wait for -


i'm new in javascript , got first problem jquery , ajax request. have many toggles on page , when click them, have ask rest service query if can or not. if can show alert box sure blablabla , if not alert says user can't.

here main problem, if use async:false works read it's not practice (and chrome remember me not) way that?

i'm using backbone , don't want every toggle set method handle click , second method handle call back.

what way of doing that?

startservice: function(servicename){         var confirm = true;          var $paid = this.model.getpaidto(servicename);         console.log(this.model.getpaidto(servicename));         //here consol.log undefined if use async: true          if ($paid == 'false'){             confirm = confirm("are sure start" + this.model.services[servicename].price + ". continue ?");         }          if(confirm){             console.log("start " + servicename + " service")             return true;         }         return false;     }, 

model:

getpaidto: function(servicename){         var self = this;         var url = require.apiurl + "profile/girl/" + this.loginmodel.id + "/checkservice/" + servicename;          $.ajax({             type: "get",             url: url,             data: {},             success: function(data){                 return data.data.service;             },             error: function(data){                 self.error = 'error';                 return 'error';             }         });     } 

best regards

thanks slaks,

it's working perfectly. here way did modification:

startservice: function(servicename){         var self = this;         var confirm = true;          //var $paid = this.model.getpaidto(servicename);         this.model.getpaidto(servicename).done(function(data){             $paid = data.data.service;              if ($paid == 'false'){                 confirm = window.confirm("are sure start" + self.model.services[servicename].price + ". continue ?");             }              if(confirm){                 console.log("start " + servicename + " service");             }          }).fail(function(){             alert('error');         });     }, 

model:

getpaidto: function(servicename){         var url = require.apiurl + "profile/girl/" + this.loginmodel.id + "/checkservice/" + servicename;          return $.ajax({             type: "get",             url: url         });     } 

hope helps other people.

best regards


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -