2

I would like to interact with a REST API using $resource but when calling the save method I get has no method '$save' error. My code is inspired by the answer found on AngularJS $resource RESTful example

myapp.factory('Monitoring', function($resource) {
   return $resource('http://localhost:8080/wepapp/network/v1/cronjobs/:id', { id: '@id' } );
});

Q1: What is the purpose of @ in { id: '@id' }? I found it in most of the examples.

myapp.factory('MonitoringCRUDControllerService', ['Monitoring', function(Monitoring) {
    return {
        create: function(id, command, schedule) {
            console.log("create");
            console.log(command);
            console.log(schedule);
            Monitoring.id = id;
            Monitoring.command = command;
            Monitoring.schedule = schedule;
            console.log(Monitoring);
            Monitoring.$save();
        }
    }
}]);

The Monitoring object is correctly injected:

function Resource(value){
        copy(value || {}, this);
      } 

Calling the $save failed with the error has no method '$save'.

Q2: What the purpose of $ before save?

Q3: What am I missing to make the save method work?

Community
  • 1
  • 1
Sydney
  • 11,964
  • 19
  • 90
  • 142

1 Answers1

1

You need to create an instance of your Monitoring class like this (not tested):

var m = new Monitoring({id:id});
m.command = command;
m.schedule = schedule;
m.$save();

The documentation (http://docs.angularjs.org/api/ngResource.$resource) has a similar example:

var newCard = new CreditCard({number:'0123'});
newCard.name = "Mike Smith";
newCard.$save();

I asked the same question about the @ sign a while ago: "at" sign in parameter names in resource definition. Basically @ sign means that the value will be read from the object's property. Or as the same documentation says:

If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object (useful for non-GET operations).

The $ is not any special character, it is just a part of the method name.

Community
  • 1
  • 1
akonsu
  • 28,824
  • 33
  • 119
  • 194
  • The error is gone but I noticed that in Chrome, the called URL does not contains the port number and the method type is OPTIONS: `OPTIONS http://localhost/wepapp/network/v1/cronjobs`. Any reason for that behaviour? Does `$resource` handles JSONP? – Sydney Aug 23 '13 at 09:49
  • I do not think that JSONP would work. I think angular expects JSON data from the URL. I am not sure why it omits the port number. The documentation says that port numbers are respected. Try running it on the default port and see if it works. – akonsu Aug 23 '13 at 09:57
  • For the port I needed to escape `:` with '\\:' – Sydney Aug 23 '13 at 10:16
  • do you use the latest angular? – akonsu Aug 23 '13 at 14:40
  • 1
    the reason why it did `OPTIONS` before you escaped the colon was because without the port number this is a different domain, so the browser was sending a so called CORS "preflight" request. – akonsu Aug 23 '13 at 15:50