38

I´m using AngularJS 1.1.3 to use the new $resource with promises...

How can I get the callback from that? I tried the same way I did with $http :

$resource.get('...').
  success(function(data, status) {
      alert(data);
   }).
   error(function(data, status) {
      alert((status);
   });

But there is no 'success' neither 'error' functions...

I also tried that :

$resource.get({ id: 10 },function (data) {
   console.log('success, got data: ', data);
 }, function (err) {
   alert('request failed');
 });

That always print "success, got data" even if the return is a 404 ...

Any idea?

Thanks

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
Paul
  • 12,359
  • 20
  • 64
  • 101

3 Answers3

49

As of a PR on angulars resource and angular 1.2, angular will be switching to a simpler way of performing success / error checking. Instead of attaching callbacks or a $then method, both the Resource.get(..) and instance.get() will support the $promise method, which naturally returns a promise for both.

As of angular 1.2 the $promise feature will go live: $promise changes

Change your "get" request to be something along these lines (original example is on angularjs.org front page):

factory('Project', function($resource) {
  var Project = $resource('https://api.mongolab.com/api/1/databases' +
      '/youraccount/collections/projects/:id',
      { apiKey: 'yourAPIKey' }, {
        update: { method: 'PUT' }
      }
  );

  Project.prototype.update = function(cb) {
    return Project.update({id: this._id.$oid})
      .$promise.then(
        //success
        function( value ){/*Do something with value*/},
        //error
        function( error ){/*Do something with error*/}
      )
  };

  Project.prototype.destroy = function(cb) {
    return Project.remove({id: this._id.$oid})
      .$promise.then(
        //success
        function( value ){/*Do something with value*/},
        //error
        function( error ){/*Do something with error*/}
      )
  };

  return Project;
});

Somewhere else in the a controller you may instantiate a resource "Project" instance where you can use the same interface for successes and errors:

var myProject = new Project();

myProject.$get({id: 123}).
   .$promise.then(
      //success
      function( value ){/*Do something with value*/},
      //error
      function( error ){/*Do something with error*/}
   )
Ryan Q
  • 10,273
  • 2
  • 34
  • 39
  • 1
    I'm using 1.2.1 in [this fiddle](http://jsfiddle.net/sonicblis/5tcQa/3/), but I'm getting an error indicating $promise is undefined. 1.2.1 should conform to this answer's details, correct? – sonicblis Jun 04 '14 at 02:32
  • Just started a new projects that use's Odata and anguarl resources. Exactly what I was looking for with a nice example, thank you. – Jon D Feb 26 '15 at 12:45
29
var MyResource = $resource("/my-end-point/:action", {}, {
    getSomeStuff: { method:"GET", params: { action:"get-some-stuff" }, isArray: true },
    getSingleThing: { method:"GET", params: { action:"get-single-thing" }, isArray: false }
});

function MyController(MyResource) {
    var itemList = MyResource.getSomeStuff({}, function success() {}, function err() {});
    // will call: /my-end-point/get-some-stuff
    // will be array. each object is resource's instance
    var item = MyResource.getSingleThing({id:123}, function success() {}, function err() {});
    // will call: /my-end-point/get-single-thing?id=123
    // will be object. an instance of resource
}

Also check out the example in docs: ngResource

Umur Kontacı
  • 35,403
  • 8
  • 73
  • 96
  • A good reason *not* to use this is that an error within the success function won't get caught by the second error function, as it would normally do with a promise. Ex: ```User.getSubscriptions().$promise.then((res) => { doesNotExist123(); alert('succeeded') }).catch((e) => { alert('failed'); console.log(e); })``` Under a promise construction, failed will alert, as I believe it should. – Augie Gardner Jul 20 '17 at 05:25
0

Two ways

var resource = $resource("");
resource.$promise.then(function(data){
// do stuff success
}, function(error){
//do stuff error
});

Other way

var resource = $resource("");
resource({}, function success(data){
//do stuff
}, function error(error){
//do stuff
}
Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49