Search code examples
angularjsmeanjs

Multiple functions in service not allowing for module creation


I have a CRUD module activities instead of the default articles but the structure of the module is the same.

When I select to go to the page where I create a new activity, activities.create state is invoked (like the articles one)

.state('activities.create', {
    url: '/create',
    templateUrl: 'modules/activities/client/views/form-activity.client.view.html',
    controller: 'ActivitiesController',
    controllerAs: 'vm',
    resolve: {
        activityResolve: newActivity
    },
    data: {
        roles: ['user', 'admin'],
        pageTitle: 'Activities Create'
    }
})

function newActivity(ActivitiesService) {
    return new ActivitiesService().getActivitiesOfAllUsers();
}

And my ActivitiesService looks like this:

function ActivitiesService($resource, Authentication) {
    return {
        getActivitiesOfCurrentUser: function() {
            return $resource('api/:userId/activities/:activityId', {
                userId: Authentication.user._id,
                activityId: '@_id'
            }, {
                update: {
                    method: 'PUT'
                }
            });
        },
        getActivitiesOfAllUsers: function() {
            return $resource('api/activities/:activityId', {
                activityId: '@_id'
            }, {
                update: {
                    method: 'PUT'
                }
            });
        }
    }
}

The default setup the ActivitiesService was:

function ActivitiesService($resource, Authentication) {
    return $resource('api/:userId/activities/:activityId', {
        userId: Authentication.user._id,
        activityId: '@_id'
    }, {
        update: {
            method: 'PUT'
        }
    });
}

And the newActivity function would look like this:

function newActivity(ActivitiesService) {
    return new ActivitiesService();
}

So when the service is in the default layout, going to the create page works just fine.

However, adding another function to the service as I have done is breaking it. When I select to go to the create page, there are no errors and no indications anywhere as to why nothing is happening.

From what I can see, the activityResolve should evaluate the same and I cannot figure out why it's not working.


Solution

  • Based on the accepted answer here, I found the answer. I amended the ActivitiesService to this:

    function ActivitiesService($resource, Authentication) {
        return {
            getActivitiesOfCurrentUser: $resource('api/:userId/activities/:activityId', {
                    userId: Authentication.user._id,
                    activityId: '@_id'
                }, {
                    update: {
                        method: 'PUT'
                    }
                }),
            getActivitiesOfAllUsers: $resource('api/activities/:activityId', {
                    activityId: '@_id'
                }, {
                    update: {
                        method: 'PUT'
                    }
                })
        };
    }
    

    And changed the newActivity function to:

    function newActivity(ActivitiesService) {
        return new ActivitiesService.getActivitiesOfAllUsers;
    }
    

    (so without the parentheses on ActivitiesService)