Search code examples
angularjsngresource

Trying to make a $resource call from a controller through a model to a service, can't get the response back to the controller


I've got a controller that has a dependency on a model. It calls

AccountModel.saveAccount(vm.Account,function(data){
            response = data;
        },function(data){
            response = data;
        });

The model calls a service's save function:

return AccountService.save(request,function(resp){
            return resp;
        },function(resp){
            return resp;
        });

and that function looks like this:

function save(Account) {
        return AccountResource.save(Account, function (resp) {
            return resp;
        }, function (resp) {
            return resp;
        });

    }

The idea is that the save function will return the response to the model which will return it to the controller, which will do other stuff later. However, none of this is happening.

response (in the controller) remains undefined. How can I fix this?


Solution

  • Angular's service works a little different, the save method returns and object:

    {$promise: promisveValue, $resolve: something}
    

    So to use it properly:

    myResource.save(resouceObj, handler);
    

    or

    myResource.save().$promise.then(handler);
    

    This is a draft of what you should do, try to integrate this into your code.