What is the difference between Class actions and Instance actions in AngularJs? -


from docs:

class actions return empty instance (with additional properties below). instance actions return promise of action

the documentations doesn't differentiate between class actions , instance actions. please point out differences example if possible?

when create new resource type, supply list of actions can performed. default, these get, save, query, delete, , remove (i think remove alias of delete). can add own, says in docs.

the thing class vs instance in regard convenience of use. "class actions" refers calling action off resource class create itself, kinda static or shared methods in other languages. useful entry point getting, querying, or saving instance of resource when don't have instance. get , query clearest example of this. if have car resource, , need retrieve it, start? class action, of course, such get.

now, when resource class retrieves existing instance, or create new instance, $resource extend instance actions defined resource, prefix them $ don't collide fields on resource. these instance actions. difference between instance , class, instance done in context of current instance. if get instance, call $save or $delete on instance, save or delete instance specifically. instance actions there convenience.

so pretty same, difference context in being used.

function($resource) {          // first let's define new resource car, cars have id field    // calling $resource create new resource class can used     // create, retrieve, update, or delete instances    // done service , injected controllers needed.    var car = $resource('/api/car/:id', {id: '@id'});     // car class created has class actions can query or car instances    // let's create new instance of car , save    var newcar = new car({make: 'toyota', model: 'prius'});    // prototype of car includes instance action versions of actions defined resource. below, $save instance action    newcar.$save(); // server respond object after it's saved, can access id. let's id returned 24, we'll reference value later.      // now, let's imagine time later want retrieve car , update    // car.get class action requests resource server, parses json object, , merges car instance prototype have instance actions    // let's car created previously.    // remember, done asynchronously, our work in success handler provide    car.get({id: 24}, function(mycar) {        // let's update car have it. let's set year of model , color        mycar.year = 2004;        mycar.color = 'white';         // now, let's save our changes calling instance action $save        mycar.$save();    });      // now, let's query cars , array    // query class function expects array of resource returned    car.query(function(cars) {         // trivial example, we're going enumerate cars found , log info them        for(var = 0; < cars.length; i++)           console.log('found ' + cars[0].color + ' ' cars[0].year + ' ' + cars[0].make + ' ' + cars[0].model);    });      // ok, let's delete car created earlier. use class action delete    car.delete({id: 24}); } 

you can technically call actions either class or instance, become obvious awkward use instance actions , vise versa. example, while technically can use query instance action, wouldn't in practice because it's work , it's awkward (you'd have new car().$query(). that's silly. car.query() easier , makes more sense). so, usage in example above represents normal usage.


update:

save vs $save

$save similar save, assumes data want submit during save itself, since $save instance action. particularly useful because after response received, it'll update object returned http endpoint. if service saves object additional values populated on server side, such id, sends object json, $save update instance returned json object.

 var car = new car({make: 'toyota', model: 'prius'});  // @ point there no id property, make , model  car.$save(function() {     // angular async, need success handler continue explanation      // assuming server assigned id , sent resulting object json, can access id off original object     console.log(car.id); // has value  }); 

you similar class method, it's awkward, particularly if other code in controller needs reference car working on it

 car.save({make: 'toyota', model: 'prius'}, function(car) {        // ok, have id        console.log(car.id);  }); 

or

 var car = new car({...});  car.save(car, function(newcar) {       car = newcar; // wut? that's awkward  }); 

save could useful during instances saving small object, or performing sort of "fire , forget". anyways, use save myself.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -