Skip to content

Instantly share code, notes, and snippets.

@headwinds
Last active January 2, 2016 20:29
Show Gist options
  • Save headwinds/8357621 to your computer and use it in GitHub Desktop.
Save headwinds/8357621 to your computer and use it in GitHub Desktop.
Global service involving multiple routes - you may wish to get another model beside the main user without using global variables
// this is building on top of the excellent work by
// https://github.com/linnovate/mean/compare/no-window-user
// I simply added error handling and the ability to access multiple routes
'use strict';
//Global service for global variables
angular.module('mean.system').factory("Global", ['$http',
function($http) {
var self = this;
return {
getModelOne : function(){
self.data = {
userPromise: $http.get('/route/to/modelone')
.success(function(user) {
// note this result may not be a user - whatever your data is...
if (user) {
self.data.user = user;
self.data.authenticated = true;
}
})
.error(function(error) {
self.error = error;
})
};
return self.data;
},
getModelTwo : function(){
self.data = {
userPromise: $http.get('/route/to/modeltwo')
.success(function(user) {
if (user) {
self.data.user = user;
self.data.authenticated = true;
}
})
.error(function(error) {
self.error = error;
})
};
return self.data;
}
}
}]);
'use strict';
angular.module('mean.system').controller('IndexController', ['$scope', 'Global', function ($scope, Global) {
$scope.user = Global.getModelOne(); // or getModelTwo() which ever you need...
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment