-
-
Save RickEyre/5ad067b22049860e63f2 to your computer and use it in GitHub Desktop.
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
function UserFactory($http) { | |
var factory = {}; | |
var users = {}; | |
factory.load = function load() { | |
// Return promise returned by $http.get | |
return $http.get('http://localhost:3000/users') | |
.success(function(data) { | |
angular.forEach(data, function(item, index) { | |
users[data.id] = data; | |
}); | |
}) | |
.error(function(error, status) { | |
alert(error); | |
}); | |
}; | |
factory.getUsers = function getUsers() { | |
return users; | |
}; | |
factory.getUser = function getUser(id) { | |
return users[id]; | |
}; | |
return factory; | |
} | |
angular.module('User').factory('UserFactory', UserFactory); |
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('User').config(['$routeProvider', | |
function($routeProvider) { | |
$routeProvider. | |
when('/users', { | |
templateUrl: 'partials/users/list.html', | |
controller: 'UserListController', | |
resolve: function(UserFactory) { | |
// Resolve will wait for promise returned by load to resolve before loading page | |
return UserFactory.load() | |
} | |
}) | |
.when('/users/:id', { | |
templateUrl: 'partials/users/show.html', | |
controller: 'UserShowController', | |
resolve: function(UserFactory) { | |
// Resolve will wait for promise returned by load to resolve before loading page | |
return UserFactory.load() | |
} | |
}) | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment