Open transitive-bullshit opened 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.
@grabbou thanks!
Ping?
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);
@fisch0920 Did @grabbou 's example work for you?
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
withtotalAmount
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:
And here are a few attempts at adding
count
as a custom method to allproducts
collections: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 aProducts.count
method available.