2017年8月21日 星期一

[ServiceNow] > [Widget] Passing parameters from client side to server side

In html template, there is a text field and its name is "data.num".

When c.server.update() is called, the value of "data.num" will be passed to server side.

In server side script, call "input.num" can get the value of "data.num".




Client Script

  1. Everything from the server’s data object can be accessed via c.data or $scope.data.
  2. If you change a value in the data model, use server.update() to post the entire data object to the server script as input.
    Note: After calling server.update() the client script’s data object is automatically overwritten with the server’s data object.
  3. Use c.options or scope.options to see what values were used to invoke the widget. This object is read-only and making changes will not be visible in the server script.
Note: Only client scripts set with the UI type as Both or Mobile run in Service Portal.

Server Script

  1. An empty data object is initialized.
  2. Use the input or options objects to get any data that was sent from the client controller or used to initialize the widget from the server.
  3. After the server script executes, the data object is json serialized and sent to the client controller.


html template


<div>
<!-- your widget template -->
 
<input type="text" ng-model="c.data.num"
   ng-change="c.update()"
ng-model-options="{debounce: 250}"
       />{{c.data.num}}<br><br>
 
<li class="list-group-item" ng-repeat="item in c.data.items">
    <a href="javascript:void(0)">{{item.name}}</a><span class="pull-right">{{item.price}}</span>
</li>
</div>

client script


function() {
 var c = this;
 c.update = function() {

        c.server.update().then(function(r) {           
            alert(r.a); // return from server script
        });
    };
}


server script


(function() {
 data.items = [];
 var a = input.num;
 if(!a)
 {
  a = 1;
 }
 data.a = a;
 for( var i =0;i<a;i++)
 {
  data.items.push({name:'name '+i,price:i});
 }
})();

沒有留言:

張貼留言