SHARE
TWEET

Untitled

a guest Nov 30th, 2014 137 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //(...)
  2.  
  3. .factory('utils', function () {
  4.   return {
  5.     // Util for finding an object by its 'id' property among an array
  6.     findById: function findById(a, id) {
  7.       for (var i = 0; i < a.length; i++) {
  8.         if (a[i].id == id) return a[i];
  9.       }
  10.       return null;
  11.     },
  12.  
  13.     // Util for returning a random key from a collection that also isn't the current key
  14.     newRandomKey: function newRandomKey(coll, key, currentKey){
  15.       var randKey;
  16.       do {
  17.         randKey = coll[Math.floor(coll.length * Math.random())][key];
  18.       } while (randKey == currentKey);
  19.       return randKey;
  20.     }
  21.   };
  22. })
  23.        
  24. .factory('userData', ['$http', 'utils', 'Restangular', function ($http, utils, Restangular) {
  25.  
  26.   var data = Restangular.all('users').getList().then(function(response) {
  27.     return response;
  28.   });
  29.  
  30.   var factory = {};
  31.   factory.all = function () {
  32.     return data;
  33.   };
  34.   factory.get = function (id) {
  35.     return data.then(function(){
  36.       return utils.findById(data, id);
  37.     })
  38.   };
  39.   return factory;
  40. }]);
  41.  
  42. //(...)
  43.  
  44. Users.controller('UsersController', ['userData', function($scope, $state, $stateParams, $filter, ngTableParams, userData) {
  45.        
  46.         var list = data.all(); // ERROR - data is undefined
  47.                        
  48.         $scope.tableParams = new ngTableParams({
  49.                 page: 1,            // show first page
  50.                 count: 10,          // count per page
  51.                 sorting: {
  52.                     id: 'asc'     // initial sorting
  53.                 }
  54.             }, {
  55.                 total: list.length, // length of data
  56.                 getData: function($defer, params) {
  57.                         var orderedData = params.sorting() ? $filter('orderBy')(list, params.orderBy()) : list;
  58.                 $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
  59.                 }
  60.          });           
  61.        
  62. //(...)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top