1- var app = angular . module ( 'persons' , [ 'ngGrid' , 'ui.bootstrap' ] ) ;
1+ var app = angular . module ( 'persons' , [ 'ngResource' , ' ngGrid', 'ui.bootstrap' ] ) ;
22
33// Create a controller with name personsList to bind to the html page.
44app . controller ( 'personsList' , function ( $scope , personService ) {
55 // Refresh the grid, calling the appropriate service method.
6- $scope . refreshGrid = function ( page ) {
7- personService . getAll ( page , $scope . sortInfo . fields [ 0 ] , $scope . sortInfo . directions [ 0 ] ) . success ( function ( data ) {
6+ $scope . refreshGrid = function ( ) {
7+ var listPersonsArgs = {
8+ page : $scope . persons . currentPage ,
9+ sortFields : $scope . sortInfo . fields [ 0 ] ,
10+ sortDirections : $scope . sortInfo . directions [ 0 ]
11+ } ;
12+
13+ personService . get ( listPersonsArgs , function ( data ) {
814 $scope . persons = data ;
9- } ) ;
15+ } )
1016 } ;
1117
1218 // Do something when the grid is sorted.
@@ -19,7 +25,7 @@ app.controller('personsList', function ($scope, personService) {
1925 // Watch the sortInfo variable. If changes are detected than we need to refresh the grid.
2026 // This also works for the first page access, since we assign the initial sorting in the initialize section.
2127 $scope . $watch ( 'sortInfo.fields[0]' , function ( ) {
22- $scope . refreshGrid ( $scope . persons . currentPage ) ;
28+ $scope . refreshGrid ( ) ;
2329 } , true ) ;
2430
2531 // Initialize required information: sorting, the first page to show and the grid options.
@@ -33,14 +39,6 @@ app.controller('personsList', function ($scope, personService) {
3339} ) ;
3440
3541// Service that provides persons operations
36- app . service ( 'personService' , function ( $http ) {
37- // Makes the REST request to get the data to populate the grid.
38- this . getAll = function ( page , sortFields , sortDirections ) {
39- return $http . get ( 'resources/persons' , {
40- params : {
41- page : page ,
42- sortFields : sortFields ,
43- sortDirections : sortDirections
44- } } ) ;
45- }
42+ app . factory ( 'personService' , function ( $resource ) {
43+ return $resource ( 'resources/persons' ) ;
4644} ) ;
0 commit comments