2

I am using ngresource as follows but unfortunately I am unable to access the $update, $remove and $save methods in this way. What am I doing wrong?

angular.module('myApp.services').factory('Entry', function($resource) {
  return {
      method1: $resource('/api/entries/:id', { id: '@_id' }, {
        update: {
          method: 'PUT' // this method issues a PUT request
        }
      }),
      method2: $resource('/api/entries2', {}, {

      })
});

// not working: Entries is not a function at Scope.$scope.save
var entry = new Entries({});
entry.method1.$update();
entry.method1.$save();
entry.method1.$delete();

On the other hand, this works:

angular.module('myApp.services').factory('Entry', function($resource) {
  return $resource('/api/entries/:id', { id: '@_id' }, {
    update: {
      method: 'PUT' // this method issues a PUT request
    }
  });
});

var entry = new Entries({});
entry.$update();
entry.$save();
entry.$delete();
desmondlee
  • 1,643
  • 4
  • 21
  • 33

1 Answers1

1

So your second example doing $resource('http://example.com/resource.json') is the correct usage of that construction, while the first one is not.

After executing var entry = new Entries({}); you get entry as factory instance in your controller, which has available actions that you've defined for it.

UPD

You can have multiple resources in the service - https://stackoverflow.com/a/17163459/405623. In your example you've just missed the ['ngResource'] DI in your module.

Community
  • 1
  • 1
shershen
  • 9,875
  • 11
  • 39
  • 60
  • Do you mean it is technically wrongful to return multiple resource in a service? If so, any source to backup the statement? Because my superior requested to have multiple resource return in a factory. – desmondlee Jan 31 '16 at 13:14
  • Sorry, no; my initial statement was wrong - it's achievable, just check the DI syntax – shershen Jan 31 '16 at 13:18
  • i have included the ngResource in the main module, which automatically inherited in all module. There is no problem with the resource. The point here is i can't access anything with ```$update, $save, $delete```. It's working fine if i use ```Entries.method1.update(entry), Entries.method1.save(entry)``` etc but i want to use $update, $save, $delete for simplicity and code consistency..... – desmondlee Jan 31 '16 at 13:24
  • Does your 'entry' contain actual data retrieved from the server? maybe that's the case :"When the data is returned from the server then the object is an instance of the resource class. The actions save, remove and delete are available on it as methods with the $ prefix." ... "It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data." - https://docs.angularjs.org/api/ngResource/service/$resource – shershen Jan 31 '16 at 13:33
  • Since the entry for save and update is retrieve from client side, it uses the value from the form posted by the cline. It was defined in following way: ```var entry = new Entries({entry1: this.entry1, entry2: this.entry2});``` Does this makes things wrong? Hmm btw the official doc didn't mention about using non-get instance actions with multiple resource in a factory. So i have no idea about this. – desmondlee Jan 31 '16 at 13:38