Custom method in an Angular service using $resource in Jhipster

640 views Asked by At

I don't understand the syntax to write a custom method in an Angular Service using $resource in Jhipster. After a lot of research, I am doubtful whether it's even possible.

Here is the code for Angular Service.

(function() {
    'use strict';
    angular
        .module('tealboxApp')
        .factory('Task', Task);
Task.$inject = ['$resource', 'DateUtils'];

function Task ($resource, DateUtils) {
    var resourceUrl =  'api/tasks/:id';

    return $resource(resourceUrl, {}, {
        'query': { method: 'GET', isArray: true},
        'get': {
            method: 'GET',
            transformResponse: function (data) {
                if (data) {
                    data = angular.fromJson(data);
                    data.assignedDateTime = DateUtils.convertDateTimeFromServer(data.assignedDateTime);
                    data.startDateTime = DateUtils.convertDateTimeFromServer(data.startDateTime);
                    data.endDateTime = DateUtils.convertDateTimeFromServer(data.endDateTime);
                    data.startDate = DateUtils.convertLocalDateFromServer(data.startDate);
                    data.endDate = DateUtils.convertLocalDateFromServer(data.endDate);
                }
                return data;
            }
        },
        'update': {
            method: 'PUT',
            transformRequest: function (data) {
                var copy = angular.copy(data);
                copy.startDate = DateUtils.convertLocalDateToServer(copy.startDate);
                copy.endDate = DateUtils.convertLocalDateToServer(copy.endDate);
                return angular.toJson(copy);
            }
        },
        'save': {
            method: 'POST',
            transformRequest: function (data) {
                var copy = angular.copy(data);
                copy.startDate = DateUtils.convertLocalDateToServer(copy.startDate);
                copy.endDate = DateUtils.convertLocalDateToServer(copy.endDate);
                return angular.toJson(copy);
            }
        }
    });
}
})();

I want to add a custom method like...

'getTasksWithXYZ': {
            method: 'GET',
            transformResponse: function (data) {
                if (data) {
                    data = angular.fromJson(data);
                    data.assignedDateTime = DateUtils.convertDateTimeFromServer(data.assignedDateTime);
                    data.startDateTime = DateUtils.convertDateTimeFromServer(data.startDateTime);
                    data.endDateTime = DateUtils.convertDateTimeFromServer(data.endDateTime);
                    data.startDate = DateUtils.convertLocalDateFromServer(data.startDate);
                    data.endDate = DateUtils.convertLocalDateFromServer(data.endDate);
                }
                return data;
            }
        }

I am not sure how to pass a parameter to this get method or even how to call this method in the controller. How does one do this?

1

There are 1 answers

2
Salih Şenol Çakarcı On

In your service add params

    return $resource(resourceUrl, {}, {


     'getTasksWithXYZ': {
        method: 'GET',
        params:{id:'@id'},
        transformResponse: function (data) {
            if (data) {
                data = angular.fromJson(data);
                data.assignedDateTime = DateUtils.convertDateTimeFromServer(data.assignedDateTime);
                data.startDateTime = DateUtils.convertDateTimeFromServer(data.startDateTime);
                data.endDateTime = DateUtils.convertDateTimeFromServer(data.endDateTime);
                data.startDate = DateUtils.convertLocalDateFromServer(data.startDate);
                data.endDate = DateUtils.convertLocalDateFromServer(data.endDate);
            }
            return data;
        }
    }

     })

In your controller Task.getTasksWithXYZ(id)