2015-10-19 42 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