adityazoso
adityazoso

Reputation: 514

How can I handle JSON response in Moqui inside a transition?

I am actually making a JSON-RPC call to a transition from AngularJS. As the content type of the request is application/json, the input parameters are automatically made available in the context so I don't need to handle it explicitly.

Here is my request to 'getUsers' which is in the 'tutorial' component.

function($scope, $http) {
    $http ({
        url: 'tutorial/getUsers',
        method: "POST",
        data: JSON.stringify({username:'demouser1'}),
        headers: {'Content-Type': 'application/json'}

    }).success(function(response){
        $scope.userList = response;
    });
}]);

The code for the transition is written below

<transition name="getUsers">
    <actions>
         <service-call name="Tutorial.PartyServices.get#Users" in-map="context"/>
    </actions>
    <default-response type="none"/>
</transition>

The service looks like this

<service verb="get" noun="Users">
    <in-parameters>
        <parameter name="username"/>
    </in-parameters>
    <actions>
        <entity-find entity-name="PersonAndUserAccount" list="userList">
            <search-form-inputs default-order-by="firstName,lastName,username"/>
            <econdition field-name="username" operator="equals" to-field-name="username"/>
        </entity-find>
        <script>ec.web.sendJsonResponse(userList)</script>
    </actions>
</service>

Now to send JSON response I wrote ec.web.sendJsonResponse(userList) in the service itself. This makes the service tightly coupled with service calls that expect JSON as response. If I want to internally call this service i.e. in Moqui I would have to define another service.

So my question is can I handle this response in the transition which is making this service call?

Upvotes: 2

Views: 232

Answers (1)

Pranay Pandey
Pranay Pandey

Reputation: 56

Here is the sample available for mentioning JSON response in transitions. You can refer ExampleApp.xml.

Here is the code snippet for your reference.

<transition name="ExampleEntity" method="post">
    <service-call name="org.moqui.example.ExampleServices.createExample" in-map="ec.web.parameters"
        web-send-json-response="true"/>
<default-response type="none"/>
</transition>

HTH.

Thanks

Upvotes: 4

Related Questions