SHARE
TWEET
Untitled
a guest
Nov 30th, 2014
137
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- //(...)
- .factory('utils', function () {
- return {
- // Util for finding an object by its 'id' property among an array
- findById: function findById(a, id) {
- for (var i = 0; i < a.length; i++) {
- if (a[i].id == id) return a[i];
- }
- return null;
- },
- // Util for returning a random key from a collection that also isn't the current key
- newRandomKey: function newRandomKey(coll, key, currentKey){
- var randKey;
- do {
- randKey = coll[Math.floor(coll.length * Math.random())][key];
- } while (randKey == currentKey);
- return randKey;
- }
- };
- })
- .factory('userData', ['$http', 'utils', 'Restangular', function ($http, utils, Restangular) {
- var data = Restangular.all('users').getList().then(function(response) {
- return response;
- });
- var factory = {};
- factory.all = function () {
- return data;
- };
- factory.get = function (id) {
- return data.then(function(){
- return utils.findById(data, id);
- })
- };
- return factory;
- }]);
- //(...)
- Users.controller('UsersController', ['userData', function($scope, $state, $stateParams, $filter, ngTableParams, userData) {
- var list = data.all(); // ERROR - data is undefined
- $scope.tableParams = new ngTableParams({
- page: 1, // show first page
- count: 10, // count per page
- sorting: {
- id: 'asc' // initial sorting
- }
- }, {
- total: list.length, // length of data
- getData: function($defer, params) {
- var orderedData = params.sorting() ? $filter('orderBy')(list, params.orderBy()) : list;
- $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
- }
- });
- //(...)
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.

