NgResource request with composite key parameter

I have a resource Answerthat has a composite key of QuestionnaireIdand QuestionId. The ngResource code is as follows:

function answerResource($resource) {
    return $resource("/api/answers/:questionnaireId/:questionId",
        {
            questionnaireId: "@questionnaireId",
            questionId: "@questionId"
        }
    );
}

I want to request this resource with the questionnaire ID and return all the answers. If I use:

answerResource.query(
    {
        questionnaireId: questionnaireId
    }
);

Then the requested URL:

/api/answers/123

When I want it to be:

/api/answers?questionnaireId=123

Otherwise, I have two routes that I need to process to search for the request: one with the identifier in the query string, the other with the identifier as part of the URL. (I also have queries with search text where the questionnaire identifier may be missing, which will use URLs such as /api/answers?q=sometext).

, .query , . ?

+4
3

, , search , URL-:

// normal resource definition here...
,{
    search: {
        method: "GET",
        url: "/api/answers",
        isArray: true
    }
}

, , URL-, . /api/answers?questionnaireId=123

+2

, (item, sequence).
1- html.erb: 2 https://spin.atomicobject.com/2013/11/22/pass-rails-data-angularjs/
, .js script .erb, ( " - crud" angularjs)
2- .js . html, document.getElementById getAttribute, : '/items?personId='+div.getAttribute( "data-personId" ) + & = JSON '
3- items_controller, g ,

params [: personId].present?     @items = Item.where(personId: params [: personId])        -  

, , , angularjs. , , .

0

$ , :

return $resource("/api/answers/:questionnaireId/:questionId"..

, $resource.query({param: }), , $, URL-, .

( ) , :

: https://jsfiddle.net/Nedev_so/b71feyc6/19/

: , ,

factory:

 //only for example purpose
 var answersBaseUrl = "https://example.com/api/answers";

 var answerTemplateUri = commentsBaseUrl + '/:questionnaireId/:questionId'

 var params = {questionnaireId: '@_questionnaireId',questionId: '@_questionId'};

 var  res = $resource(answerTemplateUri, params,{
   one :{
       method: "GET",
     },
   all: {
        method: "GET",
        url: answersBaseUrl,
        isArray: true
    }


  });

      return res;
});

:

//get answers by questionnaire
//GET /answers?questionnaireId=123
$scope.answersByQuestionnaire = answers.all({questionnaireId: 123});

 //get answers by question
 //GET /answers?questionId=123
 $scope.answersByQuestion = answers.all({questionId:123}); 

 //get single answer by questionnaire and question
 //GET /answers/123/123
 $scope.answer = answers.one({questionnaireId: 123,questionId:123 });

(check the network logs and you will see the expected behavior)

0
source

Source: https://habr.com/ru/post/1654095/


All Articles