Angular V 1.3.15 $resource custom PUT configuration per documentation not working

110 views Asked by At

this is the $resource configuration.

'use strict';
angular.module('MyApp')
    .factory('Doc',['$resource',function DocFactory($resource){
        return $resource('/db/doc/:dbName/:docId/:docRev',
                    null,
                    {'upDate': { method:'PUT' }}
                    );
    }]);

DELETE and GET work fine

the upDate function is called with a submit button

$scope.upDate = function(){
    Doc.upDate({dbName: 'lol-sched-teams',
                docId: $scope.team._id, 
                docRev: $scope.team._rev})
             .$promise.then(function (data) {
                 $scope.team = data;
 });
}

When I debug into angular I find that the '/db/doc/' portion of the url is working fine but the ':docId/:docRev' are not being parsed to build out the entire url.

So at the Express server the request comes in as 'req: PUT /db/doc'

?????

Obviously I am missing SOMETHING!

Any help appreciated

Problem Solved???? Well understood anyway.

This is really not well documented. Happily the angular code is beautifully written and easy to follow.

$resource REALLY WANTS 4 arguments params, data, function success(){}, function error(){}

If you debug into angular you will see this is the case on the parse of the command args So I made the PUT work by re-coding the call to the resource factory to be:

Doc.upDate({dbName: 'lol-sched-teams', 
            docId: $scope.team._id, 
            docRev:    $scope.team._rev},
           $scope.team,
           function success(data){
              console.log(data);
           },
           function error(err){
              console.log(err);
           }
          )

Within angular-resource this will now parse as a1=params, a2=data, a3=success, // is A Function a4=error // is A Function

This will be all gibberish unless you use the debugger and look at it running but when you do it will be perfectly obscure

1

There are 1 answers

0
Martin Da Rosa On
'use strict';
angular.module('MyApp')
    .factory('Doc',
        [
            '$resource',
            function DocFactory($resource){
            /*You must set de params of the resource*/
            return $resource('/db/doc/:dbName/:docId/:docRev',
                        {
                            dbName : '@dbName',
                            docId : '@docId',
                            docRev : '@docRev'
                        },
                        { 
                            upDate: { 
                                method:'PUT',
                                url : '/db/doc/:dbName/:docId/:docRev' // can change
                                params: {
                                    dbName : '@dbName',
                                    docId : '@docId',
                                    docRev : '@docRev'
                                } 
                            }
                        }
            );
            }
        ]
    );


....

/*First you must have a Doc resource*/
$scope.upDate = Doc.get({     // or $scope.upDate = new Doc();
    dbName : 'someDbName',
    docId : 'someDocId',
    docRev : 'someDocRev'
}).$promise.then(
    function(){
        //$scope.upDate is now a Doc, 

        //Now you can call it here or in other event
        $scope.upDate.$upDate({
            dbName : 'newDbName',
            docId : 'someDocId',
            docRev : 'newDocRev'
        }).then(
            function(){
                console.log("OK!")
            }
        ).error(
            function(){
                console.log("UPS!")
            }
        );


    }
);