I'm having some confusion about using ng-grid and $resource and how to
successfully (and hopefully, best) use callbacks and code with asynchronous
data. Let's say I have a backend that is giving me todo-list JSON data
like:
[{description: 'foo', assignee: { first_name: 'Steve', last_name:
'Harlow'}, due_date: '4/30/2014'}, {description: 'bar', assignee: {
first_name: 'Steve', last_name: 'Harlow' }, due_date: '4/30/2014'}]
And I want to populate it in ng-grid using $resource to fetch it. Here's
how I've formulated my controllers and services, that I thought would work
but it doesn't seem to be:
App.controller('TasksCtrl', ['$scope', 'Task', function($scope, Task) {
$scope.tasks = Task.dataForNgGrid();
$scope.columns = [{field: 'description', displayName: 'Task'}, {field:
'getFullName()', displayName: 'Name'}, {field: 'due_date', displayName:
'Due Date'}]
$scope.gridOptions = {
data: 'tasks',
columnDefs: $scope.columns
};
}]);
App.factory('Task', ['$resource', function($resource) {
return {
query: function(){ $resource('/tasks.json'(.query)},
dataForNgGrid: function(){
$resource('/tasks.json').query({}, function(tasks){
angular.forEach(tasks, function(task){
task.full_name = task.assignee.first_name + ' ' +
task.assignee.last_name;
});
return tasks;
});
}
}
}]);
This code results in a blank area where the grid should be. Even {{ tasks
}} is empty. What am I doing wrong where it comes to populating the tasks
for the ng-grid to access? (doing just the Task.query() method works).
Can this code be better organized? For instance, is a dedicated service
function (Task.dataForNgGrid) the best way to go?
Thanks!
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.