Last active
January 2, 2016 20:29
-
-
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 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
// 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; | |
} | |
} | |
}]); |
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
'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