0

I'm new to Angualrjs and I'm trying to figure out this code... this is how the service looks like

var userGroupServices = angular.module('userGroupServices', ['ngResource']);
userGroupServices.factory('UserRoles', ['$resource', function ($resource) {

    var r1 = $resource('/api/UserRoles/:UserRoleId', null, {
        'update': { method: 'PUT' },
        'delete': { method: 'DELETE' }
    });
    r2 = $resource('/api/UserRoles', null, {
        'add': { method: 'POST' },
        'getRoles': { method: 'GET' }
    });
    r3 = $resource('/api/UserRoles/GetRolesByGroupType/:groupTypeName', null, {
        'getRolesByName': {method:'GET'}
    });

    r1.getAll = r2.getRoles.bind(r2);
    r1.add = r2.add.bind(r2);
    r1.getRolesByName = r3.getRolesByName.bind(r3);
    return r1;

}]);

Why is it in the end, you bind r2 and r3 variables into r1 variable? how do i use this factory to POST something, i try to post it like this way but it didn't do anything(in my controller)...

addService.addRole({ roleName: groupTypeName });

1 Answer 1

1

I think the reason why resources 2 and 3 are being combined and returned by resource 1 is to abstract these other resources away from someone who wants to use the UserRoles service. Now you don't need to know about how many resources the service needs in order to work.

As for posting, .addRole() doesn't seem to exist in your service. Try UserRoles.add({object}).

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

1 Comment

Yeah... i figure it out it's trying to combin r2 and r3 variables to r1 because then you can use it in the controller as one feature, which means you can use any of that method names in the controller by just naming the method name, for instance,` addService.getRoles({ roleName: groupTypeName })`. thanks for the help...

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.