mgonto / restangular

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

Using customGET on base object #623

Open marcelgwerder opened 10 years ago

marcelgwerder commented 10 years ago

I need to make a request that looks like:

/api/users?first_name=John&_config=mode-count,meta-filter-count

The result of that request is an object containing only the count (because of the _config=mode-count). It's just the way I handle counts on resources in my api.

{
    "filter-count": 5
}

So I thought customGET would be the most suitable option. But it looks like customGET is not available on the base object. Restangular.customGET(); is undefined. Is it possible to make that available on the base object too or what would be the best practice to handle my problem?

wawyed commented 10 years ago

I believe what you want is: Restangular.one('users').get({first_name='John', _config : 'mode-count, meta-filter-count'}). Why do you need to use a customGET?

marcelgwerder commented 10 years ago

I've had it like that before but it just looks absurd to use one when parsing an url without an id which represents multiple datasets. And most important I had the problem that it tried to add the methods usually added to my models. And that even before the request is sent.

Restangular.extendModel('users', function(user) {
    user.addRestangularMethod('invite', 'post', 'invite');      
    return user;
});

That resulted in Object #<Object> has no method 'addRestangularMethod'.

wawyed commented 10 years ago

You can always do Restangular.one('users?first_name=John&_config=mode-count,meta-filter-count').get() so it won't extend the model of it.

marcelgwerder commented 10 years ago

Ok but what exactly is the reason not to have customGET available on the base object? I mean its a custom get request, which should in my opinion be possible from everywhere no matter on which url scructure you currently are.

Shouldn't matter if you are on /api and want to do a custom request or on /foo/1/bar/2/whatever or did I completely fail in understanding the purpose of customGET?

mgonto commented 10 years ago

You should do Restangular.all('users').customGET(..  Or Restangular.oneUrl('users', 'users').get(... Would be the right way to use it. Hope it works for you. — Sent from Mailbox for iPhone

On Thu, Mar 13, 2014 at 2:51 AM, Marcel Gwerder notifications@github.com wrote:

I need to make a request that looks like:

/api/users?first_name=John&_config=mode-count,meta-filter-count

The result of that request is an object containing only the count (because of the _config=mode-count). It's just the way I handle counts on resources in my api.

{
    "filter-count": 5
}

So I thought customGET would be the most suitable option. But it looks like customGET is not available on the base object. Restangular.customGET(); is undefined. Is it possible to make that available on the base object too or what would be the best practice to handle my problem?

Reply to this email directly or view it on GitHub: https://github.com/mgonto/restangular/issues/623

marcelgwerder commented 10 years ago

Mhh unfortunately no. Both fail with Object #<Object> has no method 'addRestangularMethod'.

Even my post fails with Object #<Object> has no method 'addRestangularMethod', shouldn't this be called after a post and not before?

Ok I see the callback function of extendModel is always called twice. The first time before a request passing an empty object and then again the way I want it to after the request. Any reason for this?

I now used the following workaround, but still strange that the function is called twice:

Restangular.extendModel('users', function(user) {
    if(angular.equals(user, {})) return user;

    user.addRestangularMethod('invite', 'post', 'invite');

    return user;
});
mgonto commented 10 years ago

I think the callback is called twice now due to $object

I'll mark this as a bug.

Can you create a plunkr with an example of that extendModel being called twice? You can use http://angularjstalk.apiary.io/movies

Thanks!

daviesgeek commented 8 years ago

Any update on this issue @marcelgwerder ?

marcelgwerder commented 8 years ago

Nope not really, didn't find the time to create a plunkr and investigate further back then, decided not to extend the models...