0

I want to fetch data in a controller that uses a factory:

controller.js

  $scope.days = [function(start, end, timezone, callback) {
      calendarFactory.query({
        userId: $scope.userId
      })
      .$promise.then(function(data) {
          angular.forEach(data,function(day){
              days.push({
                  id: day.id,
                  title: day.title,
                  start: day.start
              });
          });
          callback(days);
      });
  }];
}

services.js

.factory('calendarFactory', ['$resource', 'baseURL', function($resource, baseURL) {

  return $resource(baseURL + 'days/:id', null, {
      'update': {
          method: 'PUT'
      },
      'query': {
          method: 'GET',
          isArray: true
      }
  });

}])

A day object looks like this:

{
"start": "2017-02-09T00:00:00.000Z",
"allDay": true,
"userId": "1",
"id": "589cdc7e7ff9810a76a664ec"
}

I want to query days with a specific userId. How can I do that?

2 Answers 2

0

You will need to specify different url for each method.

Then use multiple params in your url:

'query' : { 
    url: baseURL + '/:days/:id',
    method: 'GET', 
    params:{ 
        day:'@days', 
        id: '@id'
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Based on @Yaser's answer, I came to this solution:

.factory('dayFactory', ['$resource', 'baseURL', function($resource, baseURL) {

    return $resource(baseURL + 'customers/:id/days/:daysId', null, {
            id: '@Id',
            daysId: '@daysId'
        },
        {
        'update': {
            method: 'PUT'
        },
        'query': {
            method: 'GET',
            isArray: true
        }
    });

}])

Comments

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.