M
M
Mikhail Shatilov2014-08-18 19:44:06
JavaScript
Mikhail Shatilov, 2014-08-18 19:44:06

How to distribute data processing in controllers in angular?

There is a search form, there are results. At the beginning I thought to make 1 controller for the form; 1 - for results. But how in this case to betray events in "results". Or make 1 common controller?
In general, I do not know how better.

'use strict';

site.controller('SearchFormCtrl',function($scope,$http,api,search_time){
  $scope.search_params = {
    type: 1,
    from: 'Moscow, Russia (MOW-All Airports)',
    departing: '08/22/2014',
    departing_time: 'Any',
    to: 'Kiev, Ukraine (IEV-All Airports)',
    returning: '08/23/2014',
    returning_time: 'Any',
    children: 0,
    adults: 1,
    seniors: 0,
    nonstop: 0,
    refundable: 0,
  };
  $scope.search_results = {
    count:0,
  };
  $scope.search_time = search_time.getList();
  
  $scope.clickAction = function() {
    api.searchFlights($scope.search_params,function(response) {
      $scope.search_results = response.search_results;
      $scope.search_results.count = $scope.search_results.searchResultsModel.offers.length;
      console.log(response);
      
      if(!$scope.$$phase) {
        $scope.$apply();
      }
    });
  };
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-08-18
Protko @Fesor

We need more information about the functionality of forms and search results. In general, you can make an intermediary service that will store and process filters, and issue data for another controller.

D
Dmitry Demin, 2014-08-19
@keksmen

I understand that splitting the application logic into parts is a blessing, but why cut everything so much? I don't think your controller is so big that it's absolutely necessary to split it.
Too much functional code? Take out to separate libraries / services of angular.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question