1

I'm trying to comprehend how can I modify data that $resource.query() returns which - as it turned out - is not really Promise from $q but an empty object/array to be filled when async call is done.

I defined a service where I'd like to modify data that came from $resource (filter it to be precise), but nothing gets actually filtered. I get whole array back.

I'm sure there is some trivial thing I'm missing here. Thanks for help in advance.

Here's the service (Employee is the $resource):

  factory('Report', ['Employee',
        function(Employee) {

            var query = function(id, cb) {
                return Employee.query({}, function(data) {
                    return cb(data, id);
                });
            };

            var findByManager = function(employees, employeeId) {
                return employees.filter(function(element) {
                    console.log(element);
                    return employeeId === element.managerId;
                });

            };

            return {
                query: function(employee) {
                    return query(employee.employeeId, findByManager);
                }
            }; 
        }
  ]);

EDIT

By ippi suggestion I also tried accessing underlying promise:

var query = function(id) {
            return Employee.query().$promise
                .then(function(data) {
                    return findByManager(data, id);
                });
        };

return {
    query: query,
}

In controller:

$scope.employees = Report.query(id);

But it returns object instead of an array.

Ketus
  • 123
  • 1
  • 11

1 Answers1

1

I'm not sure if this is the complete answer you are looking for, but it should help:

Employee.query({...}) is not a promise itself but you CAN access the raw $http promise like this:

Employee.query({...}).$promise.then(function(result){
    console.log(result);
});

($resource is just a wrapper around $http.)

And if you don't mind me saying so, the idea of filtering your resources on the client-side the second you retrieve them does sound as if it works against the idea with API-resources to begin with. Requesting a subset of "Employees" sure sounds like an API-feature and should probably be implemented on the server-side. I'd say you'd want to call your resource like:

Employee.query({ managerId: employee.employeeId });

Edit:

Alright, I scrapped your Report-factory (sorry!) and ended up with:

// Controller
$scope.id = 1; // Or something...
$scope.employees = Employee.query();

// Html
<tr ng-repeat="employee in employees | filter: {employeeId: id} : true | orderBy:id">

It seems like this could be an acceptable answer as well: https://stackoverflow.com/a/28907040/1497533

Community
  • 1
  • 1
ippi
  • 9,857
  • 2
  • 39
  • 50
  • Thanks for response. Question though: If I simply return findByManager from inside of then() method, will it work? – Ketus May 09 '16 at 08:35
  • About "filtering your resources on the client-side". I actually implemented API for this, but I thought that since I already have cached data on the client from which i need to get a subset, it will be more performant to simply filter instead of making whole new request for each employee.employeeId. Am i wrong? – Ketus May 09 '16 at 08:46
  • In your edit, do you have any response inside `.then(function(data) {` ? Since it's a $http-request it'll probably look like `data.data = [ { ... }, { ... }, ... ] – ippi May 09 '16 at 08:47
  • You must return something. `var query = function(id) { return Employee.... ` – ippi May 09 '16 at 09:05
  • You're correct. Unfortunately that returns object instead of an array. – Ketus May 09 '16 at 09:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111395/discussion-between-ketus-and-ippi). – Ketus May 09 '16 at 09:12