Question

I was wondering how can I set the isArray parameter in the resource, because sometimes I want it to be true and sometimes false

Here is my resource factory code

dummyServices.factory('Dummy', ['$resource',
    function ($resource) {
        return $resource('http://dummyServer/dummyApp/dummyREST/:methodName', {}, {
            query: {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                isArray: false // <-- isArray: someParameter ???
            }
        });
    }]);

I use it like this:

$scope.myDummyObj = Dummy.query(
        {
            methodName: 'dummyMethod'
        },
        myDummyArgument
    );

How can I set isArray from my Dummy.query call?

Was it helpful?

Solution

As you might already know, $resource never intended to be a replacement for a more powerful $http service, and, as such, the authors simply didn't bother exposing a more powerful and configurable API.

That said, the only way to configure the resource on run-time is to wrap it in it's own custom factory. But that just makes things uglier, IMO.

PLUNKER

app.factory('Dummy', [
  '$resource',
  function ($resource) {

    return function(config){

      return $resource('http://dummyServer/dummyApp/dummyREST/:methodName', {}, {
        query: angular.extend({
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          isArray: false // <-- isArray: someParameter ???
        }, config)
      });

    };

  }
]);

Controller:

$scope.myDummyObj = Dummy({isArray: true, method: 'GET'})
                      .query({methodName: 'dummyMethod'});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top