2015-10-16 44 views
0

我有一個名爲「ViewItem」的函數調用2個更多的功能:「成功」和「失敗」。當調用「成功」時,它會檢查一個值是否存在,如果該值存在,則返回true;如果該值不存在,則返回false。最後,我有一個名爲「PresaveAction」的第四個函數,這個函數的作用是檢查一個值是「是」還是「否」,如果是「否」,它返回true並允許我保存並且我想實現的是如果值是「是」從之前稱爲「成功」功能,並取決於「成功」返回真或假允許我保存。那麼如何將PreSaveAction函數傳遞給「成功」?Sharepoint的javascript列表表單驗證上的預測

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')){ 
 
        alert('There is an event with the same Start Date on DemoTrainingRoom2' + ' ' + item.get_item('time2') + ' - ' + currentTitle); 
 
         
 
         
 
         return true; // or item 
 
         
 
                   
 
\t \t \t \t  } 
 
\t \t \t \t } 
 

 
\t \t \t \t 
 
        return false; 
 
\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; // save file 
 
    } 
 
    
 

 
    else 
 
{ 
 
    
 
\t alert('Need to validate date'); 
 
     
 
    //here is where i need to call the result from success 
 
    
 
\t return false; // don't save file 
 
\t } 
 
\t 
 
}

@Thriggle你的建議是這樣的

var result; //global variable 
 

 
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('tiempo2')){ 
 
        alert('There is an event with the same Start Date on DemoTrainingRoom2' + ' ' + item.get_item('time2') + ' - ' + currentTitle); 
 
         
 
         var result = "Yes"; 
 
         
 
         return true; // or item 
 
         
 
                   
 
\t \t \t \t  } 
 
\t \t \t \t } 
 

 
\t \t \t \t var result = "No"; 
 
\t \t \t \t  
 
\t \t \t \t return false; 
 
\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" && result == "Yes") 
 
    { 
 
    alert(result); 
 
    //return false; 
 
} \t 
 

 
else if(SPUtility.GetSPField('demoField').GetValue() == "yes" && result == "No") 
 
    { 
 
    alert(result); 
 
    //return false; 
 
} \t 
 

 

 
}

+0

您傳遞到回調函數'context.executeQueryAsync()'將異步運行,因此您的代碼不會暫停執行,並等待這些函數的返回值;我不認爲有什麼辦法可以在執行時從這些函數中獲取返回值。接下來最好的辦法是將這些值保存到全局變量中,然後可以在'PreSaveAction()'執行時檢查它們。 – Thriggle

+0

@Thriggle thnx爲了你的利益,你會介意給我看看怎麼樣。我不知道如何在評論中格式化代碼,但不適當更新德問題與我所說的從你說什麼 – suizzak

+0

是的,你更新的是類似於我的建議,但不是'var result = ...'嘗試'這個。結果= ...,在你的成功功能裏面。當您使用'var'關鍵字時,JavaScript會在當前範圍內創建一個新的局部變量。您還需要確保您的'ViewItem()'函數在用戶嘗試保存表單之前的某個時間運行,以確保您的'result'變量在該點之前具有一個值。祝你好運! – Thriggle

回答

0

這是我做的

var originalSaveButtonClickHandler = function(){}; 
 

 
$(document).ready(function() { 
 
var saveButton = $("[name$='diidIOSaveItem']") //gets form save button and ribbon save button 
 
    if (saveButton.length > 0) { 
 
    originalSaveButtonClickHandler = saveButton[0].onclick; //save original function 
 
    } 
 
    $(saveButton).attr("onclick", "PreSaveAction2()"); //change \t \t }); 
 
}); 
 
\t //override the default PreSaveAction 
 
\t 
 
\t //custom PreSaveAction 
 
\t function PreSaveAction2(callback) { 
 
\t \t alert("iniciando validacion"); 
 
\t \t var time = SPUtility.GetSPFieldByInternalName('EventDate').GetValue(); \t 
 
\t \t if(SPUtility.GetSPField('demoField').GetValue() == "no") { 
 
\t \t \t alert('No need for validation'); 
 
\t \t \t originalSaveButtonClickHandler(); 
 
\t \t \t //return true; 
 
\t \t } 
 
\t \t else if(SPUtility.GetSPField('demoField').GetValue() == "yes") { 
 
\t \t 
 
\t \t \t var resultado1 = ViewItem('demoTrainingRoom2').then(
 
\t \t \t \t \t function(allItems) { 
 
\t \t \t \t \t \t var currentTitle = SPUtility.GetSPFieldByInternalName('EventDate').GetValue(); 
 
\t \t \t \t \t \t var res = "No"; 
 
\t \t \t \t \t \t for (var i = 0; i < allItems.get_count(); i++) { 
 
\t \t \t \t \t \t \t var item = allItems.get_item(i); 
 
\t \t \t \t \t \t \t console.log(item.get_item('tiempo2') + ' - ' + currentTitle); 
 
\t \t \t \t \t \t \t if (currentTitle == item.get_item('tiempo2')){ 
 
\t \t \t \t \t \t \t \t res = "Si"; 
 
\t \t \t \t \t \t \t \t console.log('There is an event with the same Start Date on DemoTrainingRoom2' 
 
\t \t \t \t \t \t \t \t \t + ' ' + item.get_item('tiempo2') + ' - ' + currentTitle); 
 
     \t \t \t \t \t } 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t \t if (res == "Si") { 
 
\t \t \t \t \t \t \t alert(res + " there's an event on room 2"); 
 
\t \t \t \t \t \t \t //return false; 
 
\t \t \t \t \t \t } else { 
 
\t \t \t \t \t \t \t alert(res + " no event on 2"); 
 
\t \t \t \t \t \t \t originalSaveButtonClickHandler(); 
 
\t \t \t \t \t \t \t //return true; 
 
\t \t \t \t \t \t } \t 
 
\t \t \t \t \t }, 
 
\t \t \t \t \t function (sender, args) { 
 
\t \t \t \t \t \t alert("failed. Message:" + args.get_message()); 
 
\t \t \t \t \t } 
 
\t \t \t); 
 
\t \t \t 
 
\t \t } \t \t \t \t 
 
\t } 
 

 
\t function ViewItem(listTitle) { 
 

 
\t \t var deferred = $.Deferred(); 
 

 
\t \t var context = new SP.ClientContext.get_current(); 
 
\t \t var web = context.get_web(); 
 
\t \t var list = web.get_lists().getByTitle(listTitle); 
 
\t \t var query = SP.CamlQuery.createAllItemsQuery(); 
 
\t \t var allItems = list.getItems(query); 
 
\t \t context.load(allItems, 'Include(Title, EventDate, tiempo2)'); 
 
\t \t context.executeQueryAsync(
 
\t \t \t \t Function.createDelegate(this, 
 
\t \t \t \t \t function() { deferred.resolve(allItems); }), 
 
\t \t \t \t Function.createDelegate(this, 
 
\t \t \t \t \t function (sender, args) { deferred.reject(sender, args); })); 
 
\t 
 
\t \t return deferred.promise(); 
 
\t }