How does the callback work in AngularJS mode for the REST service?

I am learning AngularJS and REST. In the sample code, the word callback used repeatedly in the authentication function. Is callback a keyword in JavaScript or Angular? Or is callback just a user variable created in this code?

How does callback work in the code below? Google callback and AngularJS do not give useful results. The code for the AngularJS module in question can be read at this link , which also contains all the code for the sample application.

Here is the code of the module itself:

 angular.module('auth', []).factory( 'auth', function($rootScope, $http, $location) { enter = function() { if ($location.path() != auth.loginPath) { auth.path = $location.path(); if (!auth.authenticated) { $location.path(auth.loginPath); } } } var auth = { authenticated : false, loginPath : '/login', logoutPath : '/logout', homePath : '/', path : $location.path(), authenticate : function(credentials, callback) { var headers = credentials && credentials.username ? { authorization : "Basic " + btoa(credentials.username + ":" + credentials.password) } : {}; $http.get('user', { headers : headers }).success(function(data) { if (data.name) { auth.authenticated = true; } else { auth.authenticated = false; } callback && callback(auth.authenticated); $location.path(auth.path==auth.loginPath ? auth.homePath : auth.path); }).error(function() { auth.authenticated = false; callback && callback(false); }); }, clear : function() { $location.path(auth.loginPath); auth.authenticated = false; $http.post(auth.logoutPath, {}).success(function() { console.log("Logout succeeded"); }).error(function(data) { console.log("Logout failed"); }); }, init : function(homePath, loginPath, logoutPath) { auth.homePath = homePath; auth.loginPath = loginPath; auth.logoutPath = logoutPath; auth.authenticate({}, function(authenticated) { if (authenticated) { $location.path(auth.path); } }) // Guard route changes and switch to login page if unauthenticated $rootScope.$on('$routeChangeStart', function() { enter(); }); } }; return auth; }); 

Additional Information

Based on @okonyk's answer, I include code from another module that calls the auth.authenticate () function:

 $scope.login = function() { auth.authenticate($scope.credentials, function(authenticated) { if (authenticated) { //do some stuff $scope.error = false; } else { $scope.error = true; } }) } 

So how does the call from login() to auth.authenticate($scope.credentials, function(authenticated) ? Is the function(authenticated) parameter sending a boolean that defines the functionality inside auth.authenticate() ? For example, true can indicate for execute a callback, while false may indicate note to execute a callback, but this will help explain this.

You can read the code in the sample application for another module using the login() method by clicking on this link .

+8
source share
2 answers

Here's a pretty good explanation:

The callback function, also known as a higher-order function, is a function that is passed to another function (allows you to call this other function "otherFunction") as a parameter, and the callback function is called (or executed) inside another. The callback function is essentially a template (established by solving a common problem), and therefore the use of the callback function is also known as the callback template.

callback not a keyword, its just the name of the parameter that is passed to the function, you can call it whatever you want ( callback or cb pretty common).

I will try to explain this with an example of a super simplified custom assembly callback function:

 function useAsCallback(string){ console.log("callback is being executed with passed parameter: " + string) } function main(param, callback){ callback(param) } main(123456, useAsCallback) 

if you run this, it will print: callback is being executed with passed parameter: 123456

The reverse pattern is commonly used to handle asynchronous JavaScript behavior.

EDIT: a more specific example:

Speaking of the code snippet ... lets say you enter your factory in the controller.

You now have an auth.authenticate method. You must pass two parameters (credentials, callback) .

 auth.authenticate({username: Joe, password: 123456}, function(authStatus){ if(authStatus){ console.log("Successfully authenticated") }else{ console.log("Access denied") } }); 

We simply passed an anonymous function as the callback parameter of our auth.authenticate method.

EDIT: response to "ADDITIONAL INFORMATION":

There seems to be some kind of misunderstanding. You are asking:

A function parameter (authenticated) sends a boolean that defines the functionality inside auth.authenticate ()

The fact is that it is completely opposite: auth.authenticate() passes the value to the (authenticated) 'function, which is an anonymous function. This happens at this point: callback && callback(auth.authenticated); - on .success or callback && callback(false); - on .error

+7
source

Mostly write something like

 callback && callback(auth.authenticated); 

or

 callback && callback(false); 

means that if this callback exists and then calls it.

simple example:

 function foo () { console.log('foo'); return 'Fighting foo!'; } foo && foo(); 

This is just a language construct, strange and not very readable. This code also implies that the result of calling foo() must be valid, but it is never processed. I would use a simple if .

0
source

Source: https://habr.com/ru/post/1240737/


All Articles