javascript - Dojo using deferred functions to get data in ajax callback function -


i have function return in function there async request holds value suppose returned function. understand nature of async request function complete , not return value while waiting on async function complete.

i attempted use dojo deferred functions have function postinformation() return value within ajax request callback. having issues , not sure issue is. under code:

dojo deferred function

function postinformation(){        var haserrors = false;         var containers = [dijit.byid("container1"), dijit.byid("container2")];         var employee = {             //data         };      var def = new dojo.deferred();      def = dojo.xhrpost({         url: 'hello',         content: employee,         load: function (data) {              formerrors = {                 "errors": true,                 "fname": "123",                 "surname": "456",                 "onames": "789",                 "bsurname": "784585"             };              //formerrors = (json.parse(data)).formerrors;              $.each(formerrors, function (key, value) {                  if (key == 'errors') {                     haserrors = value;                     //console.log('haserrors set '+value);                 }             });              if (haserrors == true) {                 (var = 0; < containers.length; i++) {                      var processingcontainer = containers[i];                      dojo.foreach(processingcontainer.getchildren(), function (wid) {                         var widgetname = wid.attr('id');                          $.each(formerrors, function (key, value) {                              if (key == widgetname && value.length > 0) {                                  var mywidget = dijit.byid(widgetname);                                 //var wdgname = dijit.byid(widgetname).attr("id");                                 var mywidgetvalue = value;                                   mywidget.validator = function () {                                     //console.log('attribute name :' + wdgname + ' error value : ' + mywidgetvalue);                                     //console.log(wdgname + " : "+mywidgetvalue);                                     this.set("invalidmessage", mywidgetvalue);                                 };                                 mywidget._hasbeenblurred = true;                                 mywidget.validate();                             }                         });                     });                 }             }              console.log(haserrors);             def.resolve(haserrors);          },         error: function(err){             console.log(err);             def.reject(err);         }      });      def.then(function(data){           console.log('in function');                //alert('in def.then , results : ' + data);          if(data == true){                         return false;          }else{return true;}      },function(err){         return false;         alert('in def.error , there has been error ' + err);     });       //return value of haserrors here }; 

i'd suggest create own deferred() object, return postinformation() function , register .then() handlers on can pick resolve or reject on own deferred object happens inside postinformation() function.

the way had creating own deferred() object, overwriting xhrpost return result meant def else , weren't returning deferred postinformation() can used outside function track progress.

function postinformation() {     var haserrors = false;     var containers = [dijit.byid("container1"), dijit.byid("container2")];     var employee = {         //data     };     var def = new dojo.deferred();     dojo.xhrpost({         url: 'hello',         content: employee,         load: function (data) {             formerrors = {                 "errors": true,                 "fname": "123",                 "surname": "456",                 "onames": "789",                 "bsurname": "784585"             };             //formerrors = (json.parse(data)).formerrors;             $.each(formerrors, function (key, value) {                 if (key == 'errors') {                     haserrors = value;                     //console.log('haserrors set '+value);                 }             });             if (haserrors == true) {                 (var = 0; < containers.length; i++) {                     var processingcontainer = containers[i];                     dojo.foreach(processingcontainer.getchildren(), function (wid) {                         var widgetname = wid.attr('id');                         $.each(formerrors, function (key, value) {                             if (key == widgetname && value.length > 0) {                                 var mywidget = dijit.byid(widgetname);                                 //var wdgname = dijit.byid(widgetname).attr("id");                                 var mywidgetvalue = value;                                 mywidget.validator = function () {                                     //console.log('attribute name :' + wdgname + ' error value : ' + mywidgetvalue);                                     //console.log(wdgname + " : "+mywidgetvalue);                                     this.set("invalidmessage", mywidgetvalue);                                 };                                 mywidget._hasbeenblurred = true;                                 mywidget.validate();                             }                         });                     });                 }             }             console.log(haserrors);             def.resolve(haserrors);         },         error: function (err) {             console.log(err);             def.reject(err);         }     });     return def.promise; };  postinformation().then(function (data) {      console.log('in function');      // process data value here contain value resolved   }, function(err)       // process error in ajax result here }); 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -