javascriptwavemaker

Service Variable Events in Wavemaker 7


I am trying to manually intercept the result of a Service Variable that consumes external JSON data.

I can bind a button to call the API and bind the resulting json data to a grid and that works fine, but I want to pre-process some of the data before manually populating the grid.

The Service Variable pop up has a tab for Events, but if you select "Javascript" for the OnSuccess or OnResult events (or any of them in fact) it doesn't generate a stub for you in the code and I cannot for the life of me work out how to trap these events.

Please could someone point me in the right direction? The documentation doesn't explain this (I've searched extensively). I will post this on the Wavemaker boards as well and will cross post the answer if I get one, too.


Solution

  • Ok, I worked it out (eventually).

    Say your web service is called wsApiCall. The IDE will create a service variable for you called WsApiCallInvoke, and this is visible in the left hand "Services" panel under "wm.ServiceVariable"

    Click this service variable to bring up the config panel, and select the "Events" tabs. Select "Javascript" for the events you want (in my case, onResult).

    ** This does NOT create a stub in the code like 6.7 does **

    EDIT : Not quite true. Further testing shows that as long as you change the owner from Application (default) to Page before you save then it looks like it does create the stub. It's possible that the Application setting creates a stub elsewhere, but if it does I've yet to find it.

    EDIT 2 : Found it :) with help on the wavemaker forum - in the files panel on the left select resources from the dropdown, and under the folder "services" there you will find app.js. The stubs are created in there for application wide events.

    You must also make sure the owner is "Page" - this doesn't seem to work if the owner is "Application". If anyone can comment as to why that would be most useful.

    So now, create your own stub in the script for your page :

    Application.$controller("MainPageController", ['$scope',
    function($scope) {
        "use strict";
    
        ...
    
        $scope.WsApiCallInvokeonSuccess = function(variable, data) {
            // Insert code here.
        };
    

    and, for me at least, it all seems to work. The data object contains the json data I require.

    Hope this helps someone.