Dojo使用延迟函数来获取ajax回调函数中的数据 [英] Dojo using deferred functions to get data in ajax callback function

查看:149
本文介绍了Dojo使用延迟函数来获取ajax回调函数中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回的函数,但是在函数中有一个异步请求,它保存假定要由函数返回的值。我明白了异步请求的性质,函数将完成,而不等待异步功能完成时返回一个值。



我尝试使用dojo延迟函数来我的函数PostInformation()返回一个值在ajax请求回调。我有一些问题,我不知道我的问题在哪里。以下是我的代码:



Dojo延期功能

  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设置为'+值);
}
});

if(hasErrors == true){
for(var i = 0; i& .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 is:'+ wdgName +'错误值为:'+ myWidgetValue);
//console.log(wdgName +:+ myWidgetValue);
this.set(invalidMessage,myWidgetValue);
};
myWidget._hasBeenBlurred = true;
myWidget.validate();
}
});
});
}
}
console.log(hasErrors);
def.resolve(hasErrors);

},
错误:function(err){
console.log(err);
def.reject(err);
}

});

def.then(function(data){

console.log('在当时的函数');
// alert('那么结果是:'+ data);
if(data == true){
return false;
} else {return true;}

} ,function(err){
return false;
alert('在def.error中出现错误'+ err);
});

//返回hasErrors的值
};


解决方案

我建议您创建自己的 Deferred()对象,从您的 PostInformation()函数返回,然后注册 .then )处理程序,以便您可以在 PostInformation()函数内发生的您自己的Deferred对象上获取解决方案或拒绝。



您创建自己的 Deferred()对象的方式,但是立即用xhrPost返回结果覆盖它这意味着 def 现在是别的,你没有从 PostInformation()返回你的Deferred,所以可以使用以外的功能跟踪进度。

  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 = {
错误: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 to'+ value);
}
});
if(hasErrors == true){
for(var i = 0; i< 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 is:'+ wdgName +'Error Value is:'+ myWidgetValue);
//console.log(wdgName +:+ myWidgetValue);
this .set(invalidMessage,myWidgetValue);
};
myWidget._hasBeenBlurred = true;
myWidget.validate();
}
});
});
}
}
console.log(hasErrors);
def.resolve(hasErrors);
},
错误:function(err){
console.log(err);
def.reject(err);
}
});
return def.promise;
};

PostInformation()然后(function(data){
console.log('then then function');
//这里将包含值的进程数据值你解决了
},function(err)
//处理ajax结果中的错误
});


I have a function with a return however in the function there is an async request which holds the value that is suppose to be returned by the function. I understand with the nature of async request the function will complete and not return a value while waiting on the async function to complete.

I attempted to use dojo deferred functions to have my function PostInformation() to return a value within the ajax request callback. I am having some issues and i am not sure where my issue is. Under is my 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 to '+value);
                }
            });

            if (hasErrors == true) {
                for (var i = 0; i < 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 is :' + wdgName + ' Error Value is : ' + 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 the then function');       
        //alert('In the def.then and the results is : ' + data);
         if(data == true){           
             return false;
         }else{return true;}

    },function(err){
        return false;
        alert('In the def.error and there has been an error ' + err);
    }); 

    //return the value of hasErrors here
};

解决方案

I'd suggest this where you create your own Deferred() object, return it from your PostInformation() function and then register .then() handlers on it so you can pick up the resolve or reject on your own Deferred object that happens inside the PostInformation() function.

The way you had it you were creating your own Deferred() object, but then immediately overwriting it with the xhrPost return result which meant def is now something else and you weren't returning your Deferred from PostInformation() so it can be used outside that function to track the 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 to '+value);
                }
            });
            if (hasErrors == true) {
                for (var i = 0; i < 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 is :' + wdgName + ' Error Value is : ' + 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 the then function');
     // process data value here which will contain the value you resolved with
  }, function(err)
      // process an error in the ajax result here
});

这篇关于Dojo使用延迟函数来获取ajax回调函数中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆