Tuesday, 6 August 2013

Best practice to manage server side search with Angular Restangular

Best practice to manage server side search with Angular Restangular

I have 2 controllers. A ManageCtrl to show a list of items and SearchCtrl
one to manage a search box with various filtering options. On search I
broadcast an event that I currently listen/act on in the ListCtrl. But I
would like to clean this up because in the ListCtrl I am not happy how
this is handled.
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();
}])
So I would like to refactor this to using it in a resource type of way but
not sure how to handle the search (and pagination which I have also).
Basically I want to pass the items result/promise to my ManageCtrl and
have all search/filter/pagination requests be handled outside of that.

No comments:

Post a Comment