Skip to content

Instantly share code, notes, and snippets.

@jraff
Last active August 29, 2015 14:15
Show Gist options
  • Save jraff/e554a1517c85fcc51273 to your computer and use it in GitHub Desktop.
Save jraff/e554a1517c85fcc51273 to your computer and use it in GitHub Desktop.
function UserFactory($http) {
init();
var users = {};
var factory = {
getUsers: getUsers,
getUser: getUser
};
return factory;
//////////////
function init() {
$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);
});
}
function getUsers() {
return users;
};
function getUser(id) {
return users[id];
}
}
angular.module('User').factory('UserFactory', UserFactory);
angular.module('User').config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/users', {
templateUrl: 'partials/users/list.html',
controller: 'UserListController'
})
.when('/users/:id', {
templateUrl: 'partials/users/show.html',
controller: 'UserShowController'
})
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment