Created
October 22, 2013 16:03
-
-
Save kanwei/7103360 to your computer and use it in GitHub Desktop.
Tourbuzz paginator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module("tourbuzz.paginator", []) | |
.directive "paginator", ($parse) -> | |
return { | |
template: """<div ng-transclude></div>""" | |
transclude: true | |
controller: ($scope) -> | |
$scope.state = {page: 1, all: null, perPage: 10} | |
$scope.nextPage = -> $scope.state.page = parseInt($scope.state.page) + 1 | |
$scope.prevPage = -> $scope.state.page = parseInt($scope.state.page) - 1 | |
$scope.$watch("state" , (scope) -> | |
$scope.page = $scope.state.page || 1 | |
$scope.numPages = Math.ceil($scope.state.all.length / 10) if $scope.state.all? | |
$scope.range = _.range(1, Math.ceil($scope.state.all.length / 10)) if $scope.state.all? | |
$scope.currentPage = $scope.state.all?.slice( ($scope.page - 1) * $scope.state.perPage, $scope.page * $scope.state.perPage) | |
, true) | |
link: (scope, element, attrs) -> | |
scope.state.perPage = attrs.paginatorPerPage if attrs.paginatorPerPage? | |
scope.$watch(attrs.paginator, -> | |
scope.state.page = 1 | |
scope.state.all = $parse(attrs.paginator)(scope) | |
, true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment