From 945ed64c77622484d66598f6b1c10312d52dad5e Mon Sep 17 00:00:00 2001
From: Eike Cochu <eike@cochu.com>
Date: Sat, 6 Feb 2016 15:27:29 +0100
Subject: [PATCH] pagination now works with callback instead of watcher

---
 vipra-ui/app/html/articles/index.html |  2 +-
 vipra-ui/app/js/controllers.js        | 32 +++++++++++++++------------
 vipra-ui/app/js/directives.js         |  3 ++-
 3 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/vipra-ui/app/html/articles/index.html b/vipra-ui/app/html/articles/index.html
index 5560bfaf..cece9c18 100644
--- a/vipra-ui/app/html/articles/index.html
+++ b/vipra-ui/app/html/articles/index.html
@@ -8,4 +8,4 @@
   </li>
 </ul>
 
-<pagination total="articlesMeta.total" page="page" limit="limit"/>
\ No newline at end of file
+<pagination total="articlesMeta.total" page="page" limit="limit" change="change"/>
\ No newline at end of file
diff --git a/vipra-ui/app/js/controllers.js b/vipra-ui/app/js/controllers.js
index ddfd22e2..15d687b1 100644
--- a/vipra-ui/app/js/controllers.js
+++ b/vipra-ui/app/js/controllers.js
@@ -79,20 +79,18 @@
     $scope.page = Math.max($stateParams.page || 1, 1);
     $scope.limit = pageSize;
 
-    var loadedOnce = false;
-    $scope.$watch('page', function(newVal, oldVal) {
-      if(newVal != oldVal || !loadedOnce) {
-        loadedOnce = true;
-        ArticleFactory.query({
-          skip: ($scope.page-1)*pageSize,
-          limit: pageSize
-        }, function(response) {
-          $scope.articles = response.data;
-          $scope.articlesMeta = response.meta;
-          $scope.queryTime = response.$queryTime;
-        });
-      }
-    });
+    $scope.change = function(page) {
+      ArticleFactory.query({
+        skip: (page-1)*pageSize,
+        limit: pageSize
+      }, function(response) {
+        $scope.articles = response.data;
+        $scope.articlesMeta = response.meta;
+        $scope.queryTime = response.$queryTime;
+      });
+    };
+
+    $scope.change($scope.page);
 
   }]);
 
@@ -200,6 +198,9 @@
    app.controller('PaginationController', ['$scope',
     function($scope) {
 
+      if(!$scope.page)
+        $scope.page = 1;
+
       $scope.calculatePages = function() {
         var pages = [],
             max   = Math.ceil($scope.total/$scope.limit*1.0),
@@ -220,8 +221,11 @@
 
       $scope.calculatePages();
 
+      var change = $scope.change() || function() {};
+
       $scope.changePage = function(page) {
         $scope.page = page;
+        change(page);
       };
    }]);
 
diff --git a/vipra-ui/app/js/directives.js b/vipra-ui/app/js/directives.js
index 007d8c2f..4b0c64c8 100644
--- a/vipra-ui/app/js/directives.js
+++ b/vipra-ui/app/js/directives.js
@@ -49,7 +49,8 @@
       scope: {
         total: '=',
         page: '=',
-        limit: '='
+        limit: '=',
+        change: '&'
       },
       controller: 'PaginationController',
       templateUrl: 'html/directives/pagination.html'
-- 
GitLab