Using AngularJS $q promises in helper factory [duplicate] - javascript

This question already has answers here:
Caching a promise object in AngularJS service
(3 answers)
Closed 4 years ago.
I'm trying to wrap by head around when to use $q in AngularJS. I've been using it in all my services so far so when I call our Web API it works nicely. What I'm trying to do is cut down on all the calls to the API since I need the same data in multiple places and I've just been pinging the API every time I needed the data.
Right now I have a service, which gets the data and a helper file to help with related things about the data.
What I want is to use this helper factory to hold the data that's needed for every body which uses it.
I'm having trouble wrapping my head around assigning the value of data from the helper file if the data hasn't gotten back to me yet when AngularJS runs.
This is what I have so far.
(function() {
var app = angular.module("Test", []);
app.service("githubService", function($http, $q) {
var deferred = $q.defer();
this.getAccount = function() {
return $http.get('https://api.github.com/users/JonDoe');
};
});
app.factory("githubHelper", ["githubService", function(githubService) {
_gitHubInfo = {};
githubService.getAccount().then(function(data) {
_gitHubInfo = data;
});
return {
gitHubInfo: _gitHubInfo
};
}]);
app.controller("Dummy", ["$scope", "githubHelper", "githubService", function($scope, githubHelper, githubService) {
// How do I make it work this way?
$scope.value = githubHelper.gitHubInfo;
// This is what I'm using now
githubService.getAccount().then(function(data) {
$scope.value2 = data;
});
}]);
})();
.box {
border: 1px red solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="Test">
<div ng-controller="Dummy">
<div class="box">
{{value}}
</div>
<br />
<div class="box">
{{value2}}
</div>
</div>
</div>
What I want to do is just remove the githubService dependency from the Dummy controller and only have it present in the githubHelper factory so I can remove the dependency on githubService from all my controllers and instead use gitHubHelper.
What do I need to change in Dummy controller to have $scope.value be the data returned from githubService.getAccount()?

I have something like this in my project:
app.factory("githubHelper", ["githubService", function(githubService) {
var promise = null;
function getInfo() {
if (!promise) {
promise = githubService.getAccount();
}
return promise;
}
return {
getInfo: getInfo
};
}]);
githubHelper.getInfo().then(function(data) {})
githubHelper.getInfo().then(function(data) {})
githubHelper.getInfo().then(function(data) {})
...

You're almost there. The problem is that githubinfo doesn't get populated until after you access it. What you should do is almost exactly (see my comment above) what you're doing githubservice.getaccount, but in githubhelper.getaccount, instead. Set githubinfo to a $q.deferred, return githubinfo.promise from getaccount, and resolve the promise in the then
UPDATE: NOW WITH MORE CODE! (now that I'm not on mobile :-D)
(function() {
var app = angular.module("Test", []);
app.service("githubService", function($http, $q) {
this.getAccount = function() {
return $http.get('https://api.github.com/users/JonDoe');
};
});
app.factory("githubHelper", ["githubService", function(githubService) {
return {
gitHubInfo: $q(function(resolve, reject) {
githubService.getAccount().then(function(data) {
resolve(data);
}, reject);
}
};
}]);
app.controller("Dummy", ["$scope", "githubHelper",
function($scope, githubHelper) {
githubHelper.gitHubInfo.then(function(data) {
$scope.value = data;
});
}]);
})();
Now, written as-is I would never approve that as a PR for many reasons (code clarity re: return { someProp: function() { /* a bunch of code */} } .... usage of $scope should be avoided at all costs... and as mentioned, the $cacheFactory can and should handle this) , but you should be able to get the general gist of how something along these lines could work

Related

angular $watch screen size with $mdMedia from service/factory

How can i get this function:
$rootScope.$watch(function () {
return $mdMedia('md');
}, function watchCallback(newValue, oldValue) {
$scope.detectScreen = $mdMedia('max-width: 1280px');
})
which at the moment sits in my controller into a service/factory, so i can use it in multiple controllers.
.factory('MyFactory', [
'$rootScope',
'$mdMedia',
function ($rootScope, $mdMedia) {
return {
detectScreen: function (param) {
***///get the function to work from here///***
}
}
}
])
Put the function in the factory without a watch:
app.factory('MyFactory', ['$mdMedia',
function ($mdMedia) {
return {
detectScreen: function () {
return $mdMedia('max-width: 1280px');
};
}
}
]);
In the controllers, use the $doCheck Life-Cycle Hook to keep the scope variable up to date:
app.controller("myController", function(myFactory, $scope) {
this.$doCheck = function () {
$scope.detectScreen = myFactory.detectScreen();
};
});
I solved the issue my self. To whom it may concerns, here is the answer.
put this in your controller:
$scope.$watch(function () {
return CmsFactory.detectScreen();
}, function watchCallback(small) {
$scope.detectScreen = small;
})
And this in your factory:
return {
detectScreen: function () {
return $mdMedia('md');
}
}
Alternatively, you could move the $watch to the app.run() and emit an event when it changes that can be caught by your controllers.
I ran across this answer when I was doing some research. My reputation isn't high enough to post a comment, but to answer jhon dano's question to ookadoo, I think he means this:
if you inject $mdMedia into your controller, you can use a watch to update a scope variable when the screen size changes.
The watch would look like this:
$scope.ScreenIsXs = false;
$scope.$watch(function () {
return $mdMedia('xs');
}, function watchCallback(response) {
//console.log(response); //test code
$scope.ScreenIsXs = response;
});
Then you could use the $scope variable to drive an ng-if in your html:
<div ng-if="ScreenIsXs">I only show when your window is very small! </div>
This would be useful for simple applications that wouldn't required a factory/service. That said, your question directly stated that you needed a service/factory, so I'm not sure how useful the response would have been prior to you finding the solution.
Anyway, I hope this clarifies ookadoo's response for anyone else who stumbles across these answers in the future.

Factory is returning a function instead of the return value of the function in AngularJS

So, I had an application pretty much figured out until I realized that I am making way to many calls to database api and as a result the app is lagging. I am trying to set up a way to keep the current scope accurate when adding and removing items from the $scope object instead of having to request the entire list from the database every time. So I figured from all I've read that a factory is the way to go. That way I can pull the data from the database once, then after successfully adding or deleting entries through the api I can just manipulate the current scope instead of requesting the entire list again. My question is (with an example), How do I fetch a list of users from the api and add that to the scope. Then when I add or delete users from the list I can just remove them from the scope without fetching the list all over again from the api?
Here is my factory:
app.factory("UsersFactory",['Data',function(Data){
var users;
function init(){
Data.get('users')
.then(function (results) {
users = results;
});
}
init();
function getUsers(){
return users;
}
return {
getUsers:getUsers
}
}]);
And after injecting the factory in the controller:
console.log(UsersFactory.getUsers);
This returns the function but not the return value of the function.
I have also tried:
console.log(UsersFactory.getUsers());
Which returns undefined;
I would actually like to do all this in the resolve for the state (ui-router) but in the controller directly is ok too i guess.
Any help would be ap;appreciated and feel free to ask me to clarify anything. I'm not very good at clearly asking questions.
The correct way to do this is to return a promise from your factory:
app.factory("UsersFactory", ['Data', function(Data) {
var factory = {
getUsers: getUsers
};
return factory;
function getUsers() {
return Data.get('users');
}
}]);
Then, in your controller:
app.controller("UsersController", ['$scope', 'UsersFactory', function($scope, UsersFactory) {
function getResponse(response) {
$scope.users = response.data;
}
function getError(response) {
console.log(response);
}
UsersFactory.getUsers()
.then(getResponse);
.catch(getError);
}]);
Factory:
app.factory("UsersFactory",function(Data) {
UpDateUserinfo:function(){
var inf;
inf=Data.get('users');
return inf;
}
})
Controller:
app.controller("UsersController", function($scope, UsersFactory){
UsersFactory.UpDateUserinfo().$promise.then(function(data){
$scope.users=data
})
})

Loading view configuration

I would like to do something like this:
app.config(function($routeProvider){
$routeProvider.when('products/list', {
controller: 'ProductListCtrl',
templateUrl : 'products/list/view.html',
resolve : { data : function(){
...
},
loadingTemplateUrl : 'general/loader.html'
}
});
I would like to have the loading page in a different view.
This would make the code in the view and controller of every page cleaner, (no <...ng-include ng-show="loading"...>). This would also mean that I don't have to $scope.$watch the data for changes. Is there a clean solution to do something similar (not necessarily in the .config method) or an alternative library to do this?
Assuming you want to show some general template for all state transitions while the data is resolved, my suggestion is to listen to the events fired by the routing library. This allows to use one central point to handle all state transitions instead of polluting the routing config (which I think will not be that easy to do).
Please see the docs for $routeChangeStart, $routeChangeSuccess and of course $routeChangeError at the angular router docs
Maybe someone could be interested in what I did: I created a new service and a new view directive. It could seem like a lot of work, but doing this was much easier than I had expected. The new service enables me to separate the main view from the loading view, that I could reuse in all pages of the application. I also provided the possibility to configure an error template url and error controller, for when the loading failed.
The Angular $injector, $templateRequest and $controller services do most of the work. I just had to connect a directive, that depends on these services, to the right event ($locationChangeSuccess), and to the promise, retrieved (using $q.all) from the resolve object's functions. This connection was done in the route service. The service selects the right template url and comtroller, and passes it on for the directive to handle.
A shortened version (with the getCurrentConfig method left out):
RouteService:
(function () {
'use strict';
// provider:
angular.module('pikcachu')
.provider('pikaRouteService', [function () {
var routeConfigArray;
var otherwiseRouteConfig;
//configuration methods
this.when = function (url, routeConfig){
routeConfigArray.push({url: url, routeConfig: routeConfig});
return this;
}
this.otherwise = function(routeConfig){
otherwiseRouteConfig = routeConfig;
return this;
}
// service factory:
this.$get = ['$rootScope', '$location', '$q', '$injector', '$templateRequest',
function ($rootScope, $location, $q, $injector, $templateRequest) {
function RouteService() {
this.setViewDirectiveUpdateFn = function(){ /*...*/ }
function init(){
$rootScope.$on('$locationChangeSuccess', onLocationChangeSuccess);
}
function onLocationChangeSuccess(){
// get the configuration based on the current url
// getCurrentConfig is a long function, because it involves parsing the templateUrl string parameters, so it's left out for brevity
var currentConfig = getCurrentConfig($location.url());
if(currentConfig.resolve !== undefined){
// update view directive to display loading view
viewDirectiveUpdateFn(currentConfig.loadingTemplateUrl, currentConfig.loadingController);
// resolve
var promises = [];
var resolveKeys = [];
for(var resolveKey in currentConfig.resolve){
resolveKeys.push(resolveKey);
promises.push($injector.invoke(resolve[resolveKey]));
}
$q.all(promises).then(resolveSuccess, resolveError);
function resolveSucces(resolutionArray){
// put resolve results in an object
var resolutionObject = {};
for(var i = 0; i< promises.length;++i){
resolved[resolveKeys[i]] = resolutionArray[i];
}
viewDirectiveUpdateFn(currentConfig.errorTemplateUrl, currentConfig.errorController);
}
function resolveError(){
viewDirectiveUpdateFn(currentConfig.errorTemplateUrl, currentConfig.errorController);
}
}
}
init();
}
return new RouteService();
}]
})();
View directive
(function () {
'use strict';
angular.module('pikachu')
.directive('pikaView', ['$templateRequest', '$compile', '$controller', 'pikaRouteService', function ($templateRequest, $compile, $controller, pikaRouteService) {
return function (scope, jQdirective, attrs) {
var viewScope;
function init() {
pikaRouteService.listen(updateView);
}
function updateView(templateUrl, controllerName, resolved) {
if(viewScope!== undefined){
viewScope.$destroy();
}
viewScope = scope.$new();
viewScope.resolved = resolved;
var controller = $controller(controllerName, { $scope: viewScope });
$templateRequest(templateUrl).then(onTemplateLoaded);
function onTemplateLoaded(template, newScope) {
jQdirective.empty();
var compiledTemplate = $compile(template)(newScope);
jQdirective.append(compiledTemplate);
}
}
init();
};
}
]);
})();

How do I add result to my scope ng-click?

This is a relatively simple piece of code that calls a service and returns some data. I need to set the $scope with the result of the data. Is there an easy way to set this data to the scope without resorting to to binding the scope to the function in the then clause?
Angular Code
(function () {
var app = angular.module('reports', []);
var reportService = function($http, $q) {
var service = {};
service.getMenuData = function() {
var deffered = $q.defer();
$http.get('/Report/MenuData').success(function(data) {
deffered.resolve(data);
}).error(function(data) {
deferred.reject("Error getting data");
});
return deffered.promise;
}
return service;
};
reportService.$inject = ['$http', '$q'];
app.factory('reportService', reportService);
var reportMenuController =
function ($scope, $http, reportService) {
$scope.getMenuData = function(e) {
reportService.getMenuData().then(function(data) {
// Need to set the $scope in here
// However, the '$scope' is out of scope
});
}
};
reportMenuController.$inject = ['$scope', '$http', 'reportService'];
app.controller('ReportMenuController', reportMenuController);
})();
Markup
<div>
<div ng-controller="ReportMenuController">
<button ng-click="getMenuData()">Load Data</button>
</div>
</div>
There is absolutely no problem to set the $scope from within the function passed to then(). The variable is available from the enclosing scope and you can set your menu data to one of its fields.
By the way: You should consider to use then() instead of success() for your http request. The code looks much nicer because then() returns a promise:
service.getMenuData = function() {
return $http.get('/Report/MenuData').then(function(response) {
return response.data;
}, function(response) {
deferred.reject("Error getting data");
});
}
success() is deprecated by now.
I didn't notice the small detail missing in the plunker where my code was different.
(function () {
...
var reportMenuController =
function ($scope, $http, reportService) {
$scope.getMenuData = getMenuData;
function getMenuData(e) {
reportService.getMenuData().then(function(data) {
// Now I have access to $scope
});
}
};
...
})();
Notice the changes to the two lines as below:
$scope.getMenuData = getMenuData;
function getMenuData(e) {
This also begs a small question which is, "Why is it okay to set getMenuData to the $scope before it is declared?

How to dynamically bound value to a link using AngularJS

The problem that I need to generate link on the fly since the link is set in ng-repeat. I think I need to execute custom function inside ng-repeat loop which gets data from $http and pushes link to $scope.array. Then bound href to $scope.array[someIndex]....The problem I don't know if:
it's the only way
a good design
how to implement it
Example:
HTML
<div ng-repeat-start="item in items">
the link
// here execute $scope.getUrl(item ) somehow
<div class="extra-div">
<div ng-repeat-end=""></div>
Controller:
$scope.arrayOfUrls= [];
$scope.getUrl = function(url){
$http.get(url).then(
function(data){
arrayOfUrls.push(data.link);
}
)
}
How to execute getUrl during ng-repeat cycle?
PS. I cannot bound href directly to getUrl function since there is $http which eventually result in infinite digest loop.
Also promises can be returned not in order so expecting that first call to getUrl will push link to $scope.arrayOfUrls[0] is false assumption.
UPDATE:
As #Claies suggested I trie to prefetch links like this:
Contoller executes $scope.loadFeed();
$scope.loadFeed = function() {
http.jsonp('feed url').then(function(res) {
$scope.feeds = res.data.responseData.feed.entries;
$scope.feeds.forEach(function(e) {
// prefetch content and links for each feed
//hook new entryStateUrl property to feed objects
e['entryStateUrl'] = $scope.getEntryStateUrl(e.link); // e['entryStateUrl'] is undefined
})
})
}
}
$scope.getEntryStateUrl = function(inputUrl) {
$http.get(inputUrl).then(function(data) {
// do stuff
return data.link;
});
}
}
Now seems like I am trying pre-fetch urls but getting undefined for e['entryStateUrl']...
The problem maybe about assigning scope variable when $http is not done getting results... Also it seems like there are nested promises: $http.jsonp and inside it $http.get.
How to fix it?
As this requires UI enhancement, a directive would be a good approach. How about a directive like this ( JSFiddle here ). Please note that I am calling $window.open here - you can replace this with whatever the application requires. :-
todoApp.directive('todoLinks', ['$window',function ($window) {
var directive = {};
directive.restrict = 'A';
directive.transclude = 'true';
directive.scope = { ngModel: '=ngModel', jsOnClick:'&' };
directive.template = '<li ng-repeat="item in ngModel">{{item.name}}</li>';
directive.link = function ($scope, element, attributes) {
$scope.openLink = function (idx) {
$window.open($scope.ngModel[idx].link); //Replace this with what your app. requires
if (attributes.jsOnClick) {
//console.log('trigger post jsOnClick');
$scope.jsOnClick({ 'idx': idx });
}
};
};
return directive;
}]);
When the controller fills the todo items like this:-
todoApp.controller("ToDoCtrl", ['$scope','$timeout','dbService',function($scope, $timeout, dbService)
{
$scope.todo=[{"name":"google","link":"http://www.google.com"},{"name":"bing","link":"http://www.bing.com"},{"name":"altavista","link":"http://www.altavista.com"}];
}]);
Usage of this directive is simple:-
<div todo-links ng-model="todo"></div>

Categories

Resources