0

I would appreciate any help in figuring out something about the code below. Specifically, how does Angular determine that it should inject the response of the resource request into authenticationResult?

myApp.controller(...{

................ 

$scope.login = function() {

    UserService.authenticate($.param({username: $scope.username, password: $scope.password}),   
                                             function(authenticationResult) {

        ...........

} );


var services = angular.module('myApp.services', ['ngResource']);


services.factory('UserService', function($resource) {

return $resource('/restendpoint/:action', {},

    {

        authenticate: {

            method: 'POST',

            params: {'action' : 'authenticate'},

            headers : {'Content-Type': 'application/x-www-form-urlencoded'}

        }

    }

);

});

2 Answers 2

2

This is how basic JavaScript works (usage of callbacks) and the way $resource is designed.

Resource.action([parameters], [success], [error])

Resource action takes the second and third parameters as success and error callback. Go through the fine documentation, things will be pretty much clear.

Want to understand JavaScript callback, let me Google that for you. :) Or some basic inputs.

Sign up to request clarification or add additional context in comments.

Comments

0

The way callbacks usually works is the following:

function fncall(bool, callback){
    var obj;
    if(typeof bool !== 'boolean')
        callback(new Error('A boolean is expected'));
    if(bool)
        obj = {value: true};
    else
        obj = {value: false} ;
    callback(null, obj);
};

and you use it like:

fncall(false, function(result){
    //using result... this is sync..
});

In this case would be more like and errorCallback and a successCallback.. but the behaviour is similar so remember for javascript everything is a value, so a function is a value too that's why you can pass it as a variable, this is the basic behaviour of callbacks.. obviously callbacks use to be used with async operations but not necessary..

saludos.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.