ServiceNow angularjs client/server 脚本通信

ServiceNow angularjs client/server script communication

我在 ServiceNow 工作,正在创建一个显示案例列表以及打印选项的小部件。我想根据所选的案例和打印选项填充一个数组,但在客户端和服务器脚本之间传递内容时遇到问题。下面是我的 HTML 和小部件的快照:

<div class="btn-group" role="group">  
    <button id="btnGroupDrop1" type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">  
      Printing Options   <i class="fa fa-caret-down"/>   
      </button>  
      <div class="dropdown-menu" aria-labelledby="btnGroupDrop1" >  
      <li ng-repeat="print in c.docSetPrint">  
          <a class="dropdown-item" href="#">{{print.document_set_name}}</a>  
        </li>  
      </div>  
    </div>  

<table id="print_table" class="table table-striped table-hover table-responsive">  
    <thead>  
      <tr>  
        <th><input type="checkbox" id="selectAll"/></th>  
          <th>${Case Number}</th>  
          <th>${Short Description}</th>  
          <th>${Start Date}</th>  
          <th>${Work Location}</th>  
        </tr>  
      </thead>  
      <tbody>  
      <tr ng-repeat="item in c.ONCase track by $index">  
        <td><input type="checkbox" id="{{item.number}}"/></td>  
          <td>{{item.number}}</td>  
          <td>{{item.short_description}}</td>  
          <td>{{item.start_date}}</td>  
          <td>{{item.location}}</td>            
        </tr>  
      </tbody>  
    </table>

在上面的 table 中,我想遍历每个条目,如果它已被选中或选中,则 return 一个包含所有选中案例编号的数组。到目前为止,在我的客户端脚本中,我有这样的东西:

c.printDocs = function(){
    var arr = [];

    for(i=0; i<c.data.ONCase.length; i++){
        if(document.getElementById(c.data.ONCase[i].number).checked == true){
            arr.push({
                case_num: c.dataONCase.number <--??
            });
        }
    }
    c.server.get({
        action: 'print_docs',
        cases: arr
    })then(function(response) {
        // do stuff after
    });
};

我对客户端和服务器脚本之间的脚本编写方式感到很困惑。获得案例编号数组后,如何将其传递给服务器?

在客户端脚本中,您可以将要发送到服务器脚本的数据放入c.data。然后在服务器脚本上,这在 input 对象中可用。

客户端脚本

function () {
    var c = this;

    c.myFunction = function () {
        // populate the c.data object
        c.data.cases= ['CASENUM01','CASENUM02','CASENUMO3'];

        // send the c.data object to the server
        c.server.update().then(function () {
            // do cleanup if needed
            c.data.cases = [];
        });
    }
}

服务器脚本

(function() {

    // input here contains c.data from client script
    if (input && input.cases) {
        for (var i = 0; i < input.cases.length; i++) {
            // write to system log
            gs.info(input.cases[i]);
        } 
    }
});

https://serviceportal.io/communicating-between-the-client-script-and-the-server-script-of-a-widget/

上有一个很好的教程