Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between .save and $save to resource in angularjs

I've seen code that both calls $save and save to a $resource in angular.

What is the difference and when do you use either?

like image 603
happygilmore Avatar asked Feb 07 '14 13:02

happygilmore


People also ask

What is ngResource in AngularJS?

ngResource is the module of the AngularJS resource service that can be used to fetch data from back end RESTFul API. The second code snippet is a great example, the newly created module has just one dependency, which is ngResource . The next step is to inject the object called $resource into the controller/factory/etc.

What is promise in AngularJS?

Promises in AngularJS are provided by the built-in $q service. They provide a way to execute asynchronous functions in series by registering them with a promise object. {info} Promises have made their way into native JavaScript as part of the ES6 specification.

What is service in AngularJS?

In AngularJS, a service is a function, or object, that is available for, and limited to, your AngularJS application. AngularJS has about 30 built-in services. One of them is the $location service.


1 Answers

Best explanation === example :

// by writing '{ id: '@id' }' we want the id to be taken from 'id' parameter in request data, hence the '@' sign. Note that this mechanism is available for non-GET RQs only:
var Notes = $resource('/notes/:id', { id: '@id' });

var noteId = "my_note1";
// below we specify 'id' explicitly - has to be done for GET RQ:
// operations on our note are done inside callback function, just to make sure that the note is resolved:
var note = Notes.get({ id: noteId }, function () {

    // let's make some changes:
    note.topic = "A brand new topic here!";

    // save using $resource "static" action (aka "class" action). 'id' is taken from data object:
    Notes.save(note);
    // We can overwrite 'id' just like this: 
    Notes.save({ id: "some_other_noteId" }, note);

    // even more changes:
    note.body = "Blah blah blah, new boring body is here";

    // this time save using instance action. Again: 'id' is taken from data object:
    note.$save();
    // changing id with instance action? there you go:
    note.$save({ id: "yet_another_noteId" });

    // Naturally, we could just:
    note.id = "OMG_how_many_of_those_noteIds_has_he_left";
    Notes.save(note);
    // ... and with instance action:
    note.id = "OK_he_wins";
    note.$save();
});

Even custom $resource actions (defined by you) have their $-prefixed counterparts, as long as they're non-GET - see http://docs.angularjs.org/api/ngResource.$resource#example_creating-a-custom-put-request.
And no, not all actions have instance method version. What would be the point of invoking GET on an instance? From official ngResource docs:

The action methods on the class object or instance object can be invoked with the following parameters:

  • HTTP GET "class" actions: Resource.action([parameters], [success], [error])
  • non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
  • non-GET instance actions: instance.$action([parameters], [success], [error])
like image 119
vucalur Avatar answered Oct 08 '22 12:10

vucalur