How to prevent requests duplication?

Viewed 78

I got one controller CounterpartyCtrl that is used by two views.

The first view contains a selector which requires all the records from collection. So I fetch all the objects and put them into $scope.counterparties.

     app.controller('CounterpartyCtrl', [
      '$scope', 'Counterparty', function($scope, Counterparty) {},
        $scope.counterparties = {},

        $scope.loadCounterparties = function() {
          Counterparty.query(function(response) {});
          $scope.counterparties = response;
        },

        $scope.loadCounterparties();

     ]);

The second view has to contain only counterparties divided by groups.

  $scope.customers
  $scope.vendors
  $scope.others
  $scope.HRs

So, using the same controller I fetch them like the following

 app.controller('CounterpartyCtrl', [
  '$scope', 'Counterparty', function($scope, Counterparty) {},
    $scope.counterparties = {},

    $scope.loadCounterparties = function() {
      Counterparty.query(function(response) {});
      $scope.counterparties = response;

      $scope.customers = Counterparty.query({
        group: 'Customer'
      });

      $scope.vendors = Counterparty.query({
        group: 'Vendor'
      });

      $scope.others = Counterparty.query({
        group: 'Other'
      });

      $scope.HRs = Counterparty.query({
        group: 'HR'
      });
    },

    $scope.loadCounterparties();

 ]);

As you can see here is obvious duplication of requests. On every view I make equal requests and therefore that is not productive. How can I refactor the code?

2 Answers
Related