mgonto / restangular

AngularJS service to handle Rest API Restful Resources properly and easily
MIT License
7.87k stars 840 forks source link

[question] Collection Count #1057

Open transitive-bullshit opened 9 years ago

transitive-bullshit commented 9 years ago

I would like to add a count method to a restangularized collection which does a custom GET to .../count where ... is the path of the collection so far, but I've been unable to get it to work. The extendCollection with totalAmount example in the README looks analogous, but maybe I'm doing something wrong?

Here's the most bare-bones version of what I'm trying to do:

Restangular.all('products').getList({ store: store }).customGET('count')
      .then(function (count) {
        console.log('products', count)
      })

And here are a few attempts at adding count as a custom method to all products collections:

RestangularProvider.addElementTransformer('products', true, function (Products) {
    Products.addRestangularMethod('count', 'get', 'count')

    return Products
  })
angular.module('...').factory('Products', function (Restangular) {
  Restangular.extendCollection('products', function (Products) {
    Products.count = function () {
      return Products.customGET('count')
    }

    return Products
  })

  return Restangular.service('products')
})

I should note that I'm trying to silo the Restangular dependency itself into the model factories as opposed to calling Restangular throughout my codebase, so I just work with Products in this case everywhere and would really like to have a Products.count method available.

grabbou commented 9 years ago

Hi @fisch0920, I will get back to you after work later today. If anyone else is willing to help in the meantime - feel free to do so.

transitive-bullshit commented 9 years ago

@grabbou thanks!

transitive-bullshit commented 9 years ago

Ping?

grabbou commented 9 years ago

Hey, sorry for the delay. Actually, custom methods are added when promises are resolved on the values they are resolved with, so the following Readme example adjusted to your variables should work ->

// create methods for your collection
  Restangular.extendCollection('products', function(collection) {
    collection.count = function() {
      // implementation here
    };

    return collection;
  });

  var productsPromise = Restangular.all('products').getList();

  productsPromise.then(function(collection) {
    collection.totalAmount(); // invoke your custom collection method
  });

In order to have your method available on "Restangular" level, not on "model/collection" level, please use "addRestangularMethod" like so:

Restangular.extendCollection('products', function(collection) {
        // signature is (name, operation, path, params, headers, elementToPost)
        // undefined when you want a normal parameter to be passed, 
       // define something if you want to overwrite it or set a default value
       // e.g setting params object will append some default params to be set with the request
        collection.addRestangularMethod('count', 'get', 'count');

        return collection;
});

Products.count().then(function(data) { });

Note that extendCollection(route, fn) is just an alias for addElementTransformer(route, true, fn); and extendModel(route, fn) is an alias for addElementTransformer(route, false, fn);

daviesgeek commented 8 years ago

@fisch0920 Did @grabbou 's example work for you?