2014-03-02 77 views
0

我在下面的代碼中有一個語法錯誤,我只想要在回調中執行的函數,但我不確定錯誤是什麼。JavaScript回調的正確語法

應接近:

onClickCallback: UpdateBillCycleStatusToCompleted(1) 

<script type="text/javascript"> 
    SP.SOD.executeFunc("callout.js", "Callout", function() { 
     var itemCtx = {}; 
     itemCtx.Templates = {}; 
     itemCtx.BaseViewID = 'Callout'; 
     // Define the list template type 
     itemCtx.ListTemplateType = 101; 
     itemCtx.Templates.Footer = function (itemCtx) { 
      // context, custom action function, show the ECB menu (boolean) 
      return CalloutRenderFooterTemplate(itemCtx, AddCustomCompleteAction, true); 
     }; 
     SPClientTemplates.TemplateManager.RegisterTemplateOverrides(itemCtx); 
    }); 

    function AddCustomCompleteAction(renderCtx, calloutActionMenu) { 
     // Add your custom action 
     calloutActionMenu.addAction(new CalloutAction({ 
      text: "Custom Action", 
      tooltip: 'This is your custom action', 
      onClickCallback: UpdateBillCycleStatusToCompleted(1) 
     } 
     })); 

    // Show the default document library actions 
    CalloutOnPostRenderTemplate(renderCtx, calloutActionMenu); 

    // Show the follow action 
    calloutActionMenu.addAction(new CalloutAction({ 
     text: Strings.STS.L_CalloutFollowAction, 
     tooltip: Strings.STS.L_CalloutFollowAction_Tooltip, 
     onClickCallback: function (calloutActionClickEvent, calloutAction) { 
      var callout = GetCalloutFromRenderCtx(renderCtx); 
      if (!(typeof (callout) === 'undefined' || callout === null)) callout.close(); 
      SP.SOD.executeFunc('followingcommon.js', 'FollowSelectedDocument', function() { 
       FollowSelectedDocument(renderCtx); 
      }); 
     } 
    })); 
    } 

    function UpdateBillCycleStatusToCompleted(itemId) { 
     alert('Completed'); 
     //var clientContext = new SP.ClientContext.get_current(); 
     //var oList = clientContext.get_web().get_lists().getByTitle('Bill Cycles'); 
     //this.oListItem = oList.getItemById(itemId); 
     //oListItem.set_item('Bill Cycle Preparation Status', 'Completed'); 
     //oListItem.update(); 
     //clientContext.executeQueryAsync(Function.createDelegate(this, this.StatusCompletedSucceeded), Function.createDelegate(this, this.StatusCompletedFailed)); 
    } 

    function StatusCompletedSucceeded() { 
     alert('Item updated!'); 
    } 

    function StatusCompletedFailed(sender, args) { 
     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
    } 
</script> 
+0

什麼是'onClickCallback:UpdateBillCycleStatusToCompleted(1)',爲什麼它是腳本塊? –

+0

你需要*傳遞*回調函數,這意味着你必須*不要*它 – Bergi

回答

1

除非UpdateBillCycleStatusToCompleted(1)實際上return function() {...}那麼你就錯了。

onClickCallback: function() {UpdateBillCycleStatusToCompleted(1);} 

這種事情應該工作。

+0

我仍然有這個錯誤:SyntaxError:Unexpected token} –

+0

沒關係!它的工作現在 –