I'm using the angular-ui:bootstrap pagination directive on multiple views which interact with different controllers; these views/controllers often have to interact with sorting and filtering, which need to update the page size dynamically, eg:
<pagination
on-select-page="setPage(page)"
items-per-page="pageSize"
boundary-links="true"
total-items="totalItems"
page="search.page"
max-size="maxVisiblePages"
class="pagination-sm"
num-pages="numPages"
previous-text="‹"
next-text="›"
first-text="«"
last-text="»"
>
</pagination>
In each of my controllers, I must re-define these $scope functions, eg:
$scope.setPage = (page) ->
$timeout ->
$scope.search.page = page if $scope.search.page isnt page
$scope.totalItems = $scope.matchedItems.length
startIndex = (page - 1) * $scope.pageSize
$scope.images = $scope.matchedItems.slice(startIndex, startIndex + $scope.pageSize)
return
$scope.sortMatches = ->
$scope.matchedItems= $filter("orderBy")($scope.matchedItems, ['type','name'], true)
$scope.setPage 1
return
...
What is the best way to give me this sorting/filtering/pagination functionality while avoiding code repetition in both the controllers and the view? I was thinking that I could create a class which installs the $scope
functions and another directive which wraps the <pagination>
directive but I don't know if this is the best option.