angularjs - How to manipulate data from ngResource query() before returning it? -
i'm trying comprehend how can modify data $resource.query()
returns - turned out - not promise $q
empty object/array filled when async call done.
i defined service i'd modify data came $resource
(filter precise), nothing gets filtered. whole array back.
i'm sure there trivial thing i'm missing here. in advance.
here's service (employee $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 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 returns object instead of array.
i'm not sure if complete answer looking for, should help:
employee.query({...}) not promise can access raw $http promise this:
employee.query({...}).$promise.then(function(result){ console.log(result); });
($resource wrapper around $http.)
and if don't mind me saying so, idea of filtering resources on client-side second retrieve them sound if works against idea api-resources begin with. requesting subset of "employees" sure sounds api-feature , should implemented on server-side. i'd you'd want call resource like:
employee.query({ managerid: employee.employeeid });
edit:
alright, scrapped report
-factory (sorry!) , ended 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 acceptable answer well: https://stackoverflow.com/a/28907040/1497533
Comments
Post a Comment