使用Angular Restangular管理服务器端搜索的最佳实践

时间:2013-08-06 22:40:02

标签: angularjs restangular

我有2个控制器。一个ManageCtrl用于显示项目列表,SearchCtrl用于管理具有各种过滤选项的搜索框。在搜索时,我在ListCtrl中广播我当前正在侦听/操作的事件。但我想清理它,因为在ListCtrl中我不知道如何处理它。

angular.module('myApp.services', []).factory('Search', function() {
  return {text:''};
});


angular.module('myApp.controllers', [])..controller('SearchCtrl',  ['$scope', 'Search', function($scope, Search){
   $scope.search = Search;
   $scope.submitSearch = function() {
     $scope.$broadcast('onSearch');
   };
 }])

angular.module('myApp.controllers', []).controller('ManageCtrl', ['$scope', '$dialog', 'config', 'Search', 'Restangular', function($scope, $dialog, config, Search, Restangular) {

  $scope.searchFromService = Search;

  $scope.$on('onSearch', function() {
    getData();
  });

  var getData = function() {
      if($scope.search) {
        // do things to modify my query
      }
      // Note - I just switched from $http to restangular so this doesn't show the application of the filering options
      Restangular.all('orders').getList().then(function(items) {
        $scope.items = items;
      });     
    };

  getData();

}])

所以我想重构一下,以资源类型的方式使用它,但不知道如何处理搜索(以及我也有的分页)。基本上我想将项目结果/承诺传递给我的ManageCtrl,并在其之外处理所有搜索/过滤/分页请求。

0 个答案:

没有答案