Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X
How would I code a function to execute every time a service is completed? I would like to use js instead of bindings.
Solved! Go to Solution.
To execute javascript code in studio home.js when a TWX service added in the External Data would trigger a "ServiceInvokedComplete" event you can add the following in the home.js to intercept that event and run code that doesn't require leveraging the bindings in studio:
$scope.$on('myTWXService.serviceInvokeComplete', function(evt, arg)
{
// do code after myTWXService executes
});
To execute javascript code in studio home.js when a TWX service added in the External Data would trigger a "ServiceInvokedComplete" event you can add the following in the home.js to intercept that event and run code that doesn't require leveraging the bindings in studio:
$scope.$on('myTWXService.serviceInvokeComplete', function(evt, arg)
{
// do code after myTWXService executes
});
What are the two parameters that are passed into the function (i.e. evt and arg)?
This is just the OOTB function definition for the serviceInvokedComplete trigger in Studio, as far as the values I've never needed to leverage them for my usecases or know what their values are but you can use console.log() in the home.js and the values will show up in the Console tab of dev tools. ie console.log(evt)
Do you not need the name of the thing that the service is being pulled from either?
I'm not sure on that, I've never had a duplicate service name to test what happens if there are two services of the same name on different things from TWX. However you do leverage the name of the Thing to access the data of the service below is a snippet of a real implementation -> TWX Service completes, its a query so it has data from TWX the result being of type Infotable, I take what the service returns and populate some widgets and present a popup where those widgets live. SESSION_DATA is a global I declare to more easily leverage the data queried went programmatically setting widget values from other UX workflow/screens in the experience. This use case is a user scans a QR code, code value is used to query a TWX table for related data, data is presented to the user in a modal popup.
$scope.$on('queryDataTableForSerial.serviceInvokeComplete', function(evt, arg)
{
SESSION_DATA = $scope.app.mdl['myTWX_Thing.HelperThing'].svc['queryDataTableForSerial'].data;
$scope.app.params['path'] = '/Thingworx/FileRepositories/SystemRepository/Photos/MGZ11BSJAAQ.png';
$scope.view.wdg['valueDisplay-3']['value'] = SESSION_DATA[0].PartNumber;
$scope.view.wdg['valueDisplay-4']['value'] = SESSION_DATA[0].ModelNumber;
$scope.view.wdg['valueDisplay-5']['value'] = SESSION_DATA[0].BatchNumber;
$scope.view.wdg['valueDisplay-6']['value'] = SESSION_DATA[0].PackagedOn;
twx.app.fn.triggerWidgetService('popup-1', 'showpopup');
$scope.view.wdg['popup-1']['visible'] = true;
$scope.view.wdg['captureBtn']['visible'] = true;
});