angularjsangularjs-directivecoffeescriptangularjs-scopeangularjs-resource

Angular Resource parameters not passing


When making POST using a Resource Service in Angular, it appears to not be sending the parameters along with the request. The Service worked fine, before I added a second function (limit, offset) -> (which allows me to dynamically send header values for the request)

Add New Score Function

newScore = new Score()
newScore.leadership = $scope.scoreLeadership
newScore.planning = $scope.scorePlanning
newScore.attitude = $scope.scoreAttitude
newScore.accomplishment = $scope.scoreAccomplishment
newScore.total = $scope.scoreTotal
newScore.comment = $scope.scoreComment
newScore.location_id = $scope.scoreBase.id
newScore.event_id = event.id
newScore.unit_id = $scope.scoreTeam.id
newScore.save null, (data) ->
        data.unit = $scope.teams[$scope.teams.indexOf($scope.scoreTeam)]
        data.location = $scope.bases[$scope.bases.indexOf($scope.scoreBase)]
        showSuccess()
        $scope.addScore.$setPristine()
        $scope.scoreLeadership = undefined
        $scope.scorePlanning = undefined
        $scope.scoreAttitude = undefined
        $scope.scoreAccomplishment = undefined
        $scope.scoreTotal = undefined
        $scope.scoreComment = undefined
        $scope.scoreBase = undefined
        $scope.scoreTeam = undefined
        $scope.scores.push(data)

Score Service

define [
"service/services"
], (services) ->

main = ($resource, event, baseUrl) ->
    (limit, offset)->
        $resource baseUrl + "score/:id", {
            id: "@id" 
            event_id: event.id
            },
            { query: {
                method: "GET",
                headers: {'X-Response-Limit' : limit , 'X-Response-Offset' : offset},
                isArray: true
                },
            save: {
                method: "POST",
                isArray: false
                }
            }

services.factory "Score", [
    "$resource"
    "event"
    "baseUrl"
    main
]

Solution

  • I have solved this issue by removing the blank function which was passing the parameters, and adding a function to the resource it self to set the parameters from.

    limit = 15
    offset = 0
    
    res = $resource baseUrl + "person/:id/:role_id", 
        { id: "@id", role_id:"@role_id", unit_id: event.id }, 
        { query: {
            method: "GET",
            isArray: true,
            headers:
                'X-Response-Limit': () ->
                    return limit
                'X-Response-Offset': () ->
                    return offset
            }
        }
    
        res.setLimit = (_limit, _offset)->
            limit = _limit
            offset = _offset    
    
        res