Skip to content

Instantly share code, notes, and snippets.

@whisher
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save whisher/0d4d821bb1c4556064e3 to your computer and use it in GitHub Desktop.

Select an option

Save whisher/0d4d821bb1c4556064e3 to your computer and use it in GitHub Desktop.
// NB solitamente la config viene dal server che scriverà come:
var CONFIG = {/* la mia configurazione*/};
angular.module('myApp.services', [])
.constant('CONFIG',CONFIG)
.value('theme','ungranbeltheme:)')
.factory('UserFactory', function($http, $q) {
var service = {
// our factory definition
user: {},
setName: function(newName) {
service.user['name'] = newName;
},
setEmail: function(newEmail) {
service.user['email'] = newEmail;
}
};
return service;
})
.service('UserService', function() {
var self = this; // Save reference
this.user = {};
this.backendUrl = "http://localhost:3000";
this.setName = function(newName) {
self.user['name'] = newName;
}
this.setEmail = function(newEmail) {
self.user['email'] = newEmail;
}
this.save = function() {
return $http.post(self.backendUrl + '/users', {
user: self.user
});
}
})
.provider('User', function() {
this.backendUrl = "http://localhost:3000";
this.setBackendUrl = function(newUrl) {
if (newUrl) this.backendUrl = newUrl;
}
// injectables go here
this.$get = function($http) {
var self = this;
var service = {
user: {},
setName: function(newName) {
service.user['name'] = newName;
},
setEmail: function(newEmail) {
service.user['email'] = newEmail;
},
save: function() {
return $http.post(self.backendUrl+'/users',
{
user: service.user
});
}
}
return service;
}
});
angular.module('myApp', ['myApp.services'])
.config(function(UserProvider) {
UserProvider
.setBackendUrl('http://localhost:9000');
})
.controller('MainController',
function($scope, CONFIG, theme,UserFactory,UserService, User) {
console.log(CONFIG);
console.log(theme);
console.log(UserFactory);
console.log(UserService);
console.log(User);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment