Looking for any example where I can invoke engine-rest in the embedded form

My requirement at the moment is to be able to query users of a group and populate my select dropdown, I plan to use the /rest-engine/user GET request with some params.
Is there easier way?

Hi @LevinHelix ,

You can utilize the JavaScript client library to do such kinds of queries

<script cam-script type="text/form-script">
inject(['$scope', 'camAPI', function($scope, camAPI) {

	var groupName = 'group1';
	var User = camAPI.resource('user');

	User.list({memberOfGroup: groupName}, function(err, data) {
		if (!err) {
			alert('Data: ' + JSON.stringify(data));
			
		}
	});
}]);
</script>

Can I have A follow up question on this, I managed to get this user data. I put it into $scope like this:

camForm.on(‘form-loaded’, function() {

    inject(['$scope', 'camAPI', function($scope, camAPI) {

        var groupName = 'areaHead';
        var User = camAPI.resource('user');
        var areaHeadUsers

        User.list({memberOfGroup: groupName}, function(err, data) {
            if (!err) {
                alert('Data: ' + JSON.stringify(data));
                $scope.areaHeadUsers = data
            }
        });
    }]);

})

I tried to add ng-model and use ng-repeat like this:

            <select required
                    cam-variable-name="cmbApprover"
                    cam-variable-type="String"
                    ng-model="areaHeadUsers">
                <option ng-repeat="x in areaHeadUsers" value="">{{x.firstName}}</option>
            </select>

I am a angularJS noob, don’t know why ng-model cannot pickup my areaHeadUsers in $scope

Hi @LevinHelix
ng-model of select represents the selected value so it shouldn’t be mapped to the users list
and it is recommended to use ng-options instead of using ng-repeat

<select required
	cam-variable-name="cmbApprover"
	cam-variable-type="String"
	ng-model="selectedUser"
	ng-options="user.id as (user.firstName + ' ' + user.lastName) for user in users">
</select>

<script cam-script type="text/form-script">
inject(['$scope', 'camAPI', function($scope, camAPI) {

	var groupName = 'all-users';
	var User = camAPI.resource('user');

	User.list({memberOfGroup: groupName}, function(err, data) {
		if (!err) {
			alert('Data: ' + JSON.stringify(data));
			$scope.users = data;
		}
	});
}]);
</script>
1 Like

Thank you very much hassang. I need to read some angular materials soon. This will do for me now.
Much appreciated.

1 Like