在返回之前不能获得推迟/完成的承诺

时间:2014-05-12 21:04:52

标签: javascript sharepoint promise

我尝试使用此处的信息:http://www.shillier.com/archive/2013/03/04/using-promises-with-the-javascript-client-object-model-in-sharepoint-2013.aspx

问题是我找到了记录吗? 我检查变量,它是0,我调用asyncQuery并验证记录计数为1,当我返回时我期望由于deferred调用而更新globalRecCount,但它无法工作: - /

var globalRecCount = 0;

function ActivateExamSched(pwd2){
//alert("ActivateExamSched");
var deferred = $.Deferred();
var clientContext = null; 
var currentweb = null; 
var n = 0;
clientContext = new SP.ClientContext.get_current(); 
web = clientContext.get_web(); 
var SchedRec = web.get_lists().getByTitle("Exam Schedule"); 
var camlQuery = new SP.CamlQuery(); 
var q = "<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + pwd2 + "</Value></Eq></Where></Query></View>" 
camlQuery.set_viewXml(q); 
this.listItems = SchedRec.getItems(camlQuery); 
//COUNT is only available IN the executeQueryAsync success function
clientContext.load(listItems, 'Include(Exam_x0020_Generated)'); 


 alert(globalRecCount);  **//<<<<<<<<<<<<<<check here, globalRecCount is 0**

clientContext.executeQueryAsync(Function.createDelegate(this, function () { deferred.resolve(this.OnFindSchedLoadSuccess); }), Function.createDelegate(this,  function (sender, args) { deferred.reject(this.OnFindSchedLoadFailed); })); 

    alert(globalRecCount);  **//<<<<<<<<<<<<<< I expect globalRecCount to be 1, because I tried to defer & get the previous call to complete, but its still 0**


}
         function  OnFindSchedLoadSuccess(sender, args){
            var RecCount = 0;
            var clientContext = new SP.ClientContext.get_current();
            var listItemEnumerator = listItems.getEnumerator();
            RecCount = this.listItems.get_count();
            alert("recordcount = " + RecCount);
            globalRecCount = RecCount;
            alert(globalRecCount);
            if (RecCount > 0) {
                while(listItemEnumerator.moveNext()) 
                {
                    var listItem = listItemEnumerator.get_current();
                    listItem.set_item('Exam_x0020_Generated', "TRUE");
                    listItem.update();
                }

//                  clientContext.executeQueryAsync(Function.createDelegate(this, function() { UpdateTargetInReport_onUpdateQuerySucceeded(sender, args); }), Function.createDelegate(this, function(sender,args) { UpdateTargetInReport_onUpdateQueryFailed(sender, args); } )); 
            }
            return deferred.promise();
         }

        function OnFindSchedLoadFailed(sender, args) 
        {
            SP.UI.Notify.addNotification('Request Generate Exam failed. ' + args.get_message());
            alert("Failed OnFindSchedLoadFailed");
        }

0 个答案:

没有答案