Widget set catalog item variable after c.server.update()

Ryan M
Giga Guru

Widget novice here.

I have a widget that we use to dynamically display some data on a catalog item.  It monitors changes to a variable (configuration item) and then makes a server call to see if some specific data exists for that ci.  If it does it renders the HTML.  After the c.server.update() happens I also want to update a variable on the catalog item that lets me know if data was found.  (Using $scope.page.g_form.setValue('ci_data', c.data.display);)

My issue is that the value of c.data.display is the value before the most recent call to c.server.update();   How do I update my catalog item variable to a value set in the most recent run of the server script?


HTML
<div ng-if="c.data.display">CI information goes here.</div>

Client Controller
$scope.$watch(function () {
        return $scope.page.g_form.getValue('configuration_item');
    }, function (ci) {
        if (ci) {
            c.data.ci = ci;
            c.server.update();
            $scope.page.g_form.setValue('ci_data', c.data.display);
        }
    });

 

 

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron
c.server.update()

returns a Promise, so if you write the code

c.server.update();
$scope.page.g_form.setValue('ci_data', c.data.display);

the 2nd instruction will be executed as soon as the server call is issued, long before the answer arrives.

Perhaps try

c.server.update().then(onPromiseFulfilled(c, $scope.page.g_form));

function onPromiseFulfilled (c, g_form) {
	return function (data) {
		c.data.display = data.display;
		g_form.setValue('ci_data', c.data.display);
	}
}

You can also "save" a $watch by setting up an event handler:

$scope.page.g_form.$private.events.on('onChange', function (fieldName, originalValue, newValue) {
	// Replace with actual code checking the modified field and acting when necessary
	console.log('onChange', fieldName, originalValue, newValue);
});

View solution in original post

7 REPLIES 7

Should we guess what g_setValue is?

Subhendu1
Tera Contributor

I was able to figure out why g_form.SetValue wasn't working we have to define g_form in the beginning  to inspect form objects .