2015-10-19 39 views
-1

我需要做PreSaveAction内部验证,如果一个字段(demofield)等于肯定的,如果是的话称一个名为ViewItem和ViewItem检查功能,如果值存在与否并更改值结果“为”是“或”否“;结果也是一个全局变量。但由于某种原因,当我在“成功”函数内改变“结果”的值时,它并不会改变全局变量“结果”。我在做什么错误或者有什么其他方法可以使验证发生?的Javascript的Sharepoint Presaveaction总是让VAR未定义

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++){ 
 
\t \t \t \t \t var item = this.allItems.get_item(i); 
 
\t \t \t \t \t console.log(item.get_item('time2') + ' - ' + currentTitle); 
 
\t \t \t \t \t      
 
        if (currentTitle == item.get_item('time2')){ 
 
        
 
        
 
        this.result = "Yes"; //// here is where i change the value of result 
 

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

 
\t \t \t \t } 
 
\t \t \t \t 
 
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; 
 
    
 
    } 
 
    
 
    } \t 
 

 

 

 
}

UPDATE

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

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

回答

0

您的成功ViewItem()函数依赖于一个异步请求,所以你的时间的结果变量检查, ViewItem的成功函数没有执行,其结果变量是s直到不变。

您可以在代码里面ViewItem()移动到您PreSaveAction功能,使成功()自执行匿名函数,这将是负责PreSaveAction的回报。

+0

我更新的代码,你的意思是类似的东西? – suizzak