Javascript Sharepoint Presaveaction总是得到var undefined

时间:2015-10-19 19:55:13

标签: javascript validation sharepoint

如果字段(demofield)等于yes,我需要在PreSaveAction内部进行验证,如果是,则调用名为ViewItem的函数,ViewItem检查是否存在值并更改&#34的值;结果&# 34;是或否;结果也是一个全局变量。但出于某种原因,当我改变"结果"的价值时在"成功"函数它不会改变全局变量"结果"。我做错了什么或有没有其他方法来进行验证?



var result; // This is always undefined

function ViewItem()
{

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('demoTrainingRoom2');
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, 'Include(Title, EventDate, time2)');

context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}



function success() {

var currentTitle = SPUtility.GetSPFieldByInternalName('EventDate').GetValue(); 
for(var i = 0; i < this.allItems.get_count(); i++){
					 var item = this.allItems.get_item(i);
					 console.log(item.get_item('time2') + ' - ' + currentTitle );
					                      
                     if (currentTitle == item.get_item('time2')){
                    
                    
                     this.result = "Yes"; //// here is where i change the value of result

					 alert('There is an event with the same Start Date on DemoTrainingRoom2' + ' ' + item.get_item('time2') + ' - ' + currentTitle + ' ' + result);
                                         
                     return true; // or item
                     
                                                             
				      }
				  }
				  
                  this.result = "No"; // here is where i change the value of result
				  alert("No event yet");    
				  return false;
				  

				}
				
function failed(sender, args) {
alert("failed. Message:" + args.get_message());
}


function PreSaveAction()    {


var time = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
alert(time + " Current Start Time ");


if(SPUtility.GetSPField('demoField').GetValue() == "no")
{
     
     alert('No need for validation');
     return true;
    }
    

   else if(SPUtility.GetSPField('demoField').GetValue() == "yes") 
 
   {
   
   ViewItem(); 
   
   if(result == "Yes") // here is here i need the new value of result globlal
    {
  alert(result);

   return false;
   
  }
  
  else
  
  {
  alert(result); // i always get here cuz result global is undefined
 
  return true;
  
  }
  
    }	



}
&#13;
&#13;
&#13;

更新

&#13;
&#13;
function ViewItem(listTitle, Success, Error) {
	var context = new SP.ClientContext.get_current();
	var web = context.get_web();
	var list = web.get_lists().getByTitle(listTitle);
	var query = SP.CamlQuery.createAllItemsQuery();
	//variable local
	var allItems = list.getItems(query);
	context.load(allItems, 'Include(Title, EventDate, tiempo2)');
	context.executeQueryAsync(function(){
			var currentTitle = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
			var res = "No";
			for (var i = 0; i < allItems.get_count(); i++) {
				var item = allItems.get_item(i);
				console.log(item.get_item('tiempo2') + ' - ' + currentTitle );
				if (currentTitle == item.get_item('tiempo2')){
					res = "Si"; // aca segun yo le cambio el valor a result
					console.log('There is an event with the same Start Date on DemoTrainingRoom2' 
						+ ' ' + item.get_item('tiempo2') + ' - ' + currentTitle);
            		//return true; // or item
        		}
			}
			return Success(res);
		},
		Error
	);
}

function PreSaveAction() {
	var time = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
	alert(time + " Current Start Time ");
	
	if(SPUtility.GetSPField('demoField').GetValue() == "no") {
		alert('No need for validation');
		return true;
	} 
	else if(SPUtility.GetSPField('demoField').GetValue() == "yes") {
		//llamo a esta pa ver si hay items con valores duplicados
		var res2 = ViewItem('demoTrainingRoom2', function(res){
				console.log('Resultado: ' + res);
				if(res == "Si") //deberia de entrar si result fuera Si
				{
					alert(res);
					return false;
				}
				else
				{
					alert(res); //Pero siempre entra aca por que result es undefined
					return true;
				}		
			},
			function (sender, args) {
				alert("failed. Message:" + args.get_message());
			}
		);
		
		
		return res2;
	}
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您的成功ViewItem()函数依赖于异步请求,因此当您检查结果变量时,ViewItem的成功函数尚未执行,其结果变量仍未更改。

您可以将代码从ViewItem()内部移动到PreSaveAction函数中,并使success()成为负责PreSaveAction返回的自执行匿名函数。