I've a jHipster project with the 4.5.1 version (Angular 1) and it does a really nice job. However, I'm already modifying the generated frontend code, which is CRUD focused for all entities, and want to unify many of them.
That said, I would like to be able to choose which entities to alert about using UI-Bootstrap. Now, when I save EntityA which manages EntityB in the same view, I get two alerts for each of the entities. I just want to get the message about the first one.
Is there any way to do it? Or just better, to disable the automatic entity messaging and doing it by hand in the controllers?
The interpectors for the angular http requests are kept in app/blocks/interceptor. There, there's a file called notification.interceptor.js and there's no filter for the entities being displayed, so we need to configure it in some way:
(function() {
'use strict';
angular
.module('myApp')
.factory('notificationInterceptor', notificationInterceptor);
notificationInterceptor.$inject = ['$q', 'AlertService'];
function notificationInterceptor ($q, AlertService) {
var service = {
response: response
};
return service;
function response (response) {
var headers = Object.keys(response.headers()).filter(function (header) {
return header.indexOf('app-alert', header.length - 'app-alert'.length) !== -1 || header.indexOf('app-params', header.length - 'app-params'.length) !== -1;
}).sort();
var alertKey = response.headers(headers[0]);
if (angular.isString(alertKey) && alertKey.indexOf('myEntityToBeDisplayed') !== -1) {
AlertService.success(alertKey, { param : response.headers(headers[1])});
}
return response;
}
}
})();
Then, if we also want to show alerts or log the error responses from the server, the errorhandler.interceptor.js intercepts each of the error responses happening. Tuning it a bit, there's the chance to show alerts for all of them:
(function() {
'use strict';
angular
.module('myApp')
.factory('errorHandlerInterceptor', errorHandlerInterceptor);
errorHandlerInterceptor.$inject = ['$q', '$rootScope'];
function errorHandlerInterceptor ($q, $rootScope) {
var service = {
responseError: responseError
};
return service;
function responseError (response) {
if (!(response.status === 401)) {
$rootScope.$emit('myApp.httpError', response);
}
return $q.reject(response);
}
}
})();
See also:
AngularJS: Catch all response status 500
Related
I have one service that is globally storing all my data used through out my app.
GlobalDataService (GDS)
angular
.module('app.core')
.service('GlobalDataService', GlobalDataService);
GlobalDataService.$inject = ['$http', 'LineStatusService'];
function GlobalDataService($http, LineStatusService) {
var gds = this;
gds.data = {
//all my data
}
gds.data.lines = LineStatusService.getLineStatus().then...
}
And a simple crud service that handles the Status of my data.
StatusDataService (SDS)
angular
.module('app.core')
.service('LineStatusService', LineStatusService);
LineStatusService.$inject = ['$http', 'GlobalDataService'];
function LineStatusService($http, GlobalDataService) {
var service = {
getLineStatus: getLineStatus,
saveLineStatus: saveLineStatus,
...
};
function saveLineStatus (line, status, user) {
var data = {
status: {
status_id: status.status_id,
status_desc: status.status_desc
},
updated_by: user
}
return $http.post('/api/euauto/v1/delivery-status/linestatus', data)
.then(function successCallback(response) {
GlobalDataService.data[id].status = status;
return response.data;
}).catch(function errorCallback(response) {
});
}
return service;
}
The GDS has to request all Status's when the app first loads, then the Status Service handles any other data requests.
Now I understand you can't have circular dependencies, therefore my plan was to have my Controller handle the save and update using SDS and ALSO update the GDS.
Potential Solution
angular
.module('core')
.controller('MyController', MyController);
MyController.$inject = ['GlobalDataService', 'LineStatusService'];
function MyController(GlobalDataService, LineStatusService) {
function changeStatus(line, status, user) {
//do a thing
//and another
LineStatusService.saveLineStatus(line, status, user);
GlobalDataService.data.line[id] = status;
GlobalDataService.updateAllOtherData();
//etc...
}
}
The Problem
My question is, now I want to develop a new Controller which contains the exact same functionality I will now have to remember to copy the same code and business logic from my original Controller to reuse both Services.
Also, if the GDS doesn't depend on SDS it won't be able to getLineStatus() on load and each Controller in the app will have to remember to getLineStatus() on load.
Ideally all the logic and requests should be contained in one place, preferably my SDS. My GDS data should be consistent across the whole Application.
If GlobalDataService is supposed to be initialized with data when the app starts, then you could initialize it in a .run() block instead of in the service constructor. That way the GDS does not need to have the other services injected at all. The other data services can then have GDS injected without circular dependency issues.
angular
.module('app.core')
.run(function(GlobalDataService, LineStatusService) {
GlobalDataService.data.lines = LineStatusService.getLineStatus().then...
});
I have a WebAPI service that returns dynamic configuration data. Before my angular app loads I would like to call that service and load the config data into angular. JSFiddle of my attempt at doing that. My question is, after seeing the string test in the console I am seeing nothing else written into the console. How do I get test 2 and wierd wierd to appear into the console
var app = angular.module('app', [])
app.provider("ConfigService", function () {
var self = this;
self.Settings = {};
self.config = function (data) {
console.log(data);
};
this.$get =
function($http) {
return self;
};
});
angular.element(document).ready(function($http) {
console.log('test')
angular.module('app').config([
'ConfigServiceProvider',
function(configService) {
console.log('test 2')
$http.get('http://www.google.com').then(function(result) {
console.log('wierd wierd')
configService.config(result);
angular.bootstrap(document, ['app']);
})
}
]);
});
EDIT
In response to the question, why I do not run this in app.run phase instead.
In the app.run phase the app is still initializing and sometimes it loads up prior to my configuration section being completed. I wanted 100% guarantee that my config section is loaded first before any of the app is.
You can use $http outside of your angular module with angular.injector. With $http you can request the config from your server and bootstrap your app when $http's promise resolves.
JS Fiddle
Create module
var app = angular.module("app", []);
app.provider("configService", function () {
var configService = {
config: {}
};
this.setConfig = function (config) { configService.config = config; };
this.$get = function() { return configService; };
});
Function that fetches config from server
function fetchConfig() {
var $http = angular.injector(["ng"]).get("$http");
return $http.get("http://www.google.com");
}
Function that bootstraps app
function bootstrap(config) {
app.config(["configServiceProvider", function (configServiceProvider) {
configServiceProvider.setConfig(config);
}]).run(["configService", function (configService) {
//Not necessary, just to confirm everything worked
console.log("YAY! You have a config:", configService.config);
}]);
angular.bootstrap(document, ["app"])
}
Put it all together!
fetchConfig().then(
/*sucess*/function (config) { angular.element(document).ready(function () { bootstrap(config); }); },
/*fail*/ function (err) { console.log("UH OH could not retrieve config!", err); });
EDIT: Please use #StevenWexler 's answer: https://stackoverflow.com/a/37599857/5670592. It is much more correct, uses a nifty angular feature ($inject), and will provide configuration before the beginning of the bootstrap cycle.
I have updated the application with your constraints regarding blocking execution until API call is complete.
Try this: https://jsfiddle.net/6svnemu8/3/
I moved the code to the module.run(...) block. This is where all providers are available and you can use $http and your ConfigService. I kept the bootstrap call in the document ready function, and I also added the $q service so you can block execution of the application until the API call is complete. You can verify this by looking at the order of the test outputs in the console:
angular.module('app').run([
'ConfigService', '$http', '$q',
function(configService, $http, $q) {
console.log('test 2');
var deferred = $q.defer();
$http.get('/6svnemu8/2/').then(function(result) {
deferred.resolve(result);
}, function(result){
deferred.reject(result);
});
console.log("test 3");
deferred.promise.then(function(result){
console.log('wierd wierd');
configService.config(result);
}, function(result){
console.log("call failed.");
});
}
]);
Option 1 -- if you have an MVC app
In your main razor view, use JSON.Net to serialize your Model (or a property on it) to JavaScript.
<script>
window.configuration = #(Html.Raw(JsonConvert.SerializeObject(Model)))
</script>
Then put it into an angular constant so you can inject it anywhere you need it, and it's guaranteed to be there. This is the most convenient way to do it.
angular.module('YourModule').constant('configuration', window.configuration);
Option 2 -- loading it asynchronously
This service will load the configuration and cache the promise.
angular.module('YourModule').factory('configuration', ['$http', function($http) {
var configurationLoaded;
var service = {
get: get
};
function get() {
if(configurationLoaded) return configurationLoaded;
configurationLoaded = $http.get( ... );
return configurationLoaded;
}
return service;
}]);
Then anywhere you need it, you'll have to pull out properties from it like this:
angular.module('YourModule').controller('SomeController', ['configuration', function(configuration) {
var vm = this;
configuration.get().then(function(config) {
vm.someSetting = config.someSetting;
});
}]);
I'm looking for some information on the best way to retrieve data from a local JSON file and handle the response. After browsing through Stack Overflow, I have some mixed thoughts as I've seen multiple ways of doing the same thing (although no explanation on why one may or may not be preferred).
Essentially, I have an Angular app that is utilising a factory to retrieve data from a JSON file; I'm then waiting for the response to resolve in my controller before using it in my html file, similar to the below:
Option 1
Factory:
comparison.factory('Info', ['$http', function($http) {
var retrievalFile = 'retrievalFile.json';
return {
retrieveInfo: function() {
return $http.get(retrievalFile);
}
}
}]);
Controller:
comparison.controller('comparisonController', ['$scope', 'Info', function($scope, Info) {
Info.retrieveInfo().then(function(response) {
$scope.info = response.data;
});
}]);
My main point of contention is figuring out when it's best to wait for the response to resolve, or if it even matters. I'm toying with the idea of having the factory return the fulfilled promise, and wait for the controller to retrieve the data also. In my view, it's best to abstract all data retrieval out of the controller and into the factory, but I'm not sure if this extends to waiting for the actual data to be returned within the factory itself. With this in mind, I'm confused about whether to opt for option 1 or option 2 and would really appreciate some feedback from more experienced/qualified developers!
Option 2
Factory:
comparison.factory('Info', ['$http', function($http) {
var retrievalFile = 'retrievalFile.json';
return {
retrieveInfo: function() {
return $http.get(retrievalFile).then(function(response) {
return response.data;
});
}
}
}]);
Controller:
comparison.controller('comparisonController', ['$scope', 'Info', function($scope, Info) {
Info.retrieveInfo().then(function(response) {
$scope.info = response;
});
}]);
Thank you for any input/suggestions in advance!
It depends on what your controller is expecting and how you set up your application. Generally, I always go with the second option. Its because I usually have global error or success handlers in all api requests and I have a shared api service. Something like below.
var app = angular.module('app', []);
app.service('ApiService', ['$http', function($http) {
var get = function(url, params) {
$http.get(url, { params: params })
.then(handleSuccess, handleError);
};
// handle your global errors here
// implementation will vary based upon how you handle error
var handleError = function(response) {
return $q.reject(response);
};
// handle your success here
// you can return response.data or response based upon what you want
var handleSuccess = function(response) {
return response.data;
};
}]);
app.service('InfoService', ['ApiService', function(ApiService) {
var retrieveInfo = function() {
return ApiService.get(retrievalFile);
/**
// or return custom object that your controller is expecting
return ApiService.get.then(function(data) {
return new Person(data);
});
**//
};
// I prefer returning public functions this way
// as I can just scroll down to the bottom of service
// to see all public functions at one place rather than
// to scroll through the large file
return { retrieveInfo: retrieveInfo };
}]);
app.controller('InfoController', ['InfoService', function(InfoService) {
InfoService.retrieveInfo().then(function(info) {
$scope.info = info;
});
}])
Or if you are using router you can resolve the data into the controller. Both ngRouter and uiRouter support resolves:
$stateProvider.state({
name: 'info',
url: '/info',
controller: 'InfoController',
template: 'some template',
resolve: {
// this injects a variable called info in your controller
// with a resolved promise that you return here
info: ['InfoService', function(InfoService) {
return InfoService.retrieveInfo();
}]
}
});
// and your controller will be like
// much cleaner right
app.controller('InfoController', ['info', function(info) {
$scope.info = info;
}]);
It's really just preference. I like to think of it in terms of API. What is the API you want to expose? Do you want your controller to receive the entire response or do you want your controller to just have the data the response wraps? If you're only ever going to use response.data then option 2 works great as you never have to deal with anything but the data you're interested in.
A good example is the app we just wrote where I work. We have two apps: a back-end API and our front-end Angular application. We created an API wrapper service in the front-end application. In the service itself we place a .catch for any of the API endpoints that have documented error codes (we used Swagger to document and define our API). In that .catch we handle those error codes and return a proper error. When our controllers/directives consume the service they get back a much stricter set of data. If an error occurs then the UI is usually safe to just display the error message sent from the wrapper service and won't have to worry about looking at error codes.
Likewise for successful responses we do much of what you're doing in option 2. In many cases we refine the data down to what is minimally useful in the actual app. In this way we keep a lot of the data churning and formatting in the service and the rest of the app has a lot less to do. For instance, if we need to create an object based on that data we'll just do that in return the object to the promise chain so that controllers aren't doing that all over the place.
I would choose option two, as it your options are really mostly the same. But let see when we add a model structure like a Person suppose.
comparison.factory('Info', ['$http', function($http) {
var retrievalFile = 'retrievalFile.json';
return {
retrieveInfo: function() {
return $http.get(retrievalFile).then(function(response) {
//we will return a Person...
var data = response.data;
return new Person(data.name, data.age, data.gender);
});
}
}
}]);
This is really simple, but if you have to map more complex data into object models (you retrieve a list of people with their own items... etc), that's when things get more complicated, you will probably want to add a service to handle the mapping between data and models. Well you have another service DataMapper(example), if you choose your first option you will have to inject DataMapper into your controller and you will have to make your request through your factory, and map the response with the injected service. And then you probably say, Should I have all this code here? ... Well probably no.
That is an hypothetical case, something that count a lot is how you feel structuring your code, won't architecture it in a way you won't understand. And at the end take a look at this: https://en.wikipedia.org/wiki/SOLID_(object-oriented_design) and research more information about this principles but focused to javascript.
Good question. A couple of points:
Controllers should be view centric versus data centric therefore you
want remove data logic from the controller and rather have it focus
on business logic.
Models (M in MVC) are a data representation of your application and
will house the data logic. In Angular case this would be a service
or factory class as you rightfully pointed out. Why is that well for
example:
2.1 AccountsController (might have multiple data models injected)
2.1.1 UserModel
2.1.2 AuthModel
2.1.3 SubscriptionModel
2.1.4 SettingsModel
There are numerous ways to approach the data model approach, but I would say your service class should be the data REST model i.e. getting, storing, caching, validating, etc. I've included a basic example, but suggest you investigate JavaScript OOP as that will help point you in the right direction as to how to build data models, collections, etc.
Below is an example of service class to manage your data.Note I have not tested this code but it should give you a start.
EXAMPLE:
(function () {
'use strict';
ArticleController.$inject = ['$scope', 'Article'];
function ArticleController($scope, Article) {
var vm = this,
getArticles = function () {
return Article.getArticles()
.then(function (result) {
if (result) {
return vm.articles = result;
}
});
};
vm.getArticles = getArticles;
vm.articles = {};
// OR replace vm.articles with $scope if you prefer e.g.
$scope.articles = {};
$scope.userNgClickToInit = function () {
vm.getArticles();
};
// OR an init on document ready
// BUT to honest I would put all init logic in service class so all in calling is init in ctrl and model does the rest
function initArticles() {
vm.getArticles();
// OR chain
vm.getArticles()
.then(getCategories); // doesn't here, just an example
}
initArticles();
}
ArticleModel.$inject = ['$scope', '$http', '$q'];
function ArticleModel($scope, $http, $q) {
var model = this,
URLS = {
FETCH: 'data/articles.json'
},
articles;
function extract(result) {
return result.data;
}
function cacheArticles(result) {
articles = extract(result);
return articles;
}
function findArticle(id) {
return _.find(articles, function (article) {
return article.id === parseInt(id, 10);
})
}
model.getArticles = function () {
return (articles) ? $q.when(articles) : $http.get(URLS.FETCH).then(cacheArticles);
};
model.getArticleById = function (id) {
var deferred = $q.defer();
if (articles) {
deferred.resolve(findArticle(id))
} else {
model.getBookmarks().then(function () {
deferred.resolve(findArticle(id))
})
}
return deferred.promise;
};
model.createArticle = function (article) {
article.id = articles.length;
articles.push(article);
};
model.updateArticle = function (bookmark) {
var index = _.findIndex(articles, function (a) {
return a.id == article.id
});
articles[index] = article;
};
model.deleteArticle = function (article) {
_.remove(articles, function (a) {
return a.id == article.id;
});
};
}
angular.module('app.article.model', [])
.controller('ArticleController', ArticleController)
.service('Article', ArticleModel);
})()
I am new to angularJs and I am trying to implement login/logout in my application.
I have an AuthService which logs user in, and a SessionService which writes the auth token to local storage (I am using jwt)
Here the AuthService:
'use strict';
angular.module('App')
.factory('AuthService', ['ApiService', 'SessionService', '$q', '$timeout', 'jwtHelper', function (ApiService, SessionService, $q, $timeout, jwtHelper) {
// inherit
var service = Object.create(ApiService);
service.login = login;
service.logout = logout;
service.check = check;
service.user = user;
return service;
function login(credentials) {
return service.form('user.login', credentials)
.then(function success(response) {
SessionService.setToken(response.token);
return response;
});
}
function logout() {
// here we use a promise so it's easier to handle
// logout in the controller by chaining methods
var d = $q.defer();
$timeout(function () {
SessionService.setToken();
d.resolve();
}, 0);
return d.promise;
}
function check() {
var token = SessionService.getToken();
return !!token && !jwtHelper.isTokenExpired(token);
}
function user() {
return service.call('user', {cache: true});
}
}]);
The problem I am facing it's in the logout method. I have no server call to do, just clear the local storage and user is logged out, but I'd like to handle this with a promise so in the controller I can do the following:
function logout() {
AuthService.logout().then(function success() {
$state.go('login');
});
}
Is this a good way of achieving this ?
I think there is no need for a promise in your particular case, and I would design it in another way :
I would store an "authenticatedUser" inside the $rootScope, with some parameters that I might find usefull (user culture, roles, ...(or just a boolean if there is no other requirement)).
In a kind of "applicationController", I would have a $watch* looking for its value :
$rootScope.$watch('authenticatedUser', function(newVal, oldVal){
if (newVal == oldVal)
return;
if (newVal == null){ //User has been disconnected
//Remove everything from screen
//Display login form
}
});
So, inside your controller, I would just have :
function logout() {
AuthService.logout();
}
That way, if ever one day you decide to be able to logout from another controller (we never know what can happen ;-) ), you will just have to call your service, and everything will be done. There will be no need to duplicate code.
Also, there is something I don't understand in your code :
// inherit
var service = Object.create(ApiService);
In angular, every service is a singleton instanciated during angular bootstrap. Are you sure you want to override this default behaviour?
: pay attention to $watches, they cost lots of processing time during angular digest.
I'm a code newbie and trying to learn angularjs. My project is simple: get JSON data from an API and display it on a web page. I found functions to make the http request and then use the data:
app.factory('myService', function($http) {
var myService = {
async: function() {
var promise = $http.get('<URL to api>').then(function(response) {
console.log(response);
return response.data;
});
return promise;
}
};
return myService;
});
app.controller('getRealTimeBusDataCtrl', function(myService, $scope) {
myService.async().then(function(d) {
$scope.data = d;
});
});
I can then access and display the whole JSON data chunk or parts of it.
What I'd like to do is set multiple $scope variables instead of just one, but as soon as I try that, the code breaks.
What I'm trying to do:
if (d.ResponseData.Buses[1].JourneyDirection = 1) {
$scope.timeToSollentuna = d.ResponseData.Buses[1].DisplayTime;
else if (d.ResponseData.Buses[1].JourneyDirection = 2) {
$scope.timeToVallingby = d.ResponseData.Buses[1].DisplayTime;
} else if (d.ResponseData.Buses[2].JourneyDirection = 1) {
$scope.timeToSollentuna = d.ResponseData.Buses[2].DisplayTime;
} else {
$scope.timeToVallingby = d.ResponseData.Buses[2].DisplayTime;
}
}
I'm guessing the problem is how the function is set up: that it somehow limits the number of variables I can set or the number of things I can "do" after .then, but I haven't been able to figure out another way to do it.
Sorry if the answer to this question is SO obvious, but I've really tried to find it and failed.
Best regards,
The way that service is written is unnecessarily verbose. Instead, I would rewrite it like this (especially if you're starting out and learning the basics--it's good to learn good habits when starting).
app.factory('myService', ["$http", function($http){
return {
getData: function() {
return $http.get('path/to/api');
}
};
}]);
app.controller('MainCtrl', ["$scope", "myService", function($scope, myService) {
//declare your data model first
$scope.busData = undefined;
//use the service to get data
myService.getData().then(function(response) {
//success... got a response
//this is where you can apply your data
$scope.busData = response.data;
//call a function to apply if you'd like
applyBusData(response.data);
}).catch(function(response) {
//error has occurred
});
function applyBusData(data) {
if(data.Buses[1].JourneyDirection === 1) {
//etcc... etc...
} else {
//etc.. etc...
}
}
}]);