From aa21c521ebf71850f5c3196b5697d371e4096a3e Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Thu, 25 Nov 2010 09:50:07 -0800 Subject: more docs for angular.service. --- src/services.js | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/services.js b/src/services.js index d4f9ff63..91932b44 100644 --- a/src/services.js +++ b/src/services.js @@ -855,19 +855,100 @@ angularServiceInject('$xhr.cache', function($xhr){ return cache; }, ['$xhr.bulk']); + /** * @workInProgress * @ngdoc service * @name angular.service.$resource * @requires $xhr - * + * * @description * Is a factory which creates a resource object which lets you interact with * RESTful * server-side data sources. * Resource object has action methods which provide high-level behaviors without - * the need to interact with the low level $xhr or XMLHttpRequest(). - * + * the need to interact with the low level $xhr or XMLHttpRequest(). + * + *
+ // Define CreditCard class
+ var CreditCard = $resource('/user/:userId/card/:cardId',
+ {userId:123, cardId:'@id'}, {
+ charge: {method:'POST', params:{charge:true}}
+ });
+
+ // We can retrieve a collection from the server
+ var cards = CreditCard.query();
+ // GET: /user/123/card
+ // server returns: [ {id:456, number:'1234', name:'Smith'} ];
+
+ var card = cards[0];
+ // each item is an instance of CreditCard
+ expect(card instanceof CreditCard).toEqual(true);
+ card.name = "J. Smith";
+ // non GET methods are mapped onto the instances
+ card.$save();
+ // POST: /user/123/card/456 {id:456, number:'1234', name:'J. Smith'}
+ // server returns: {id:456, number:'1234', name: 'J. Smith'};
+
+ // our custom method is mapped as well.
+ card.$charge({amount:9.99});
+ // POST: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'J. Smith'}
+ // server returns: {id:456, number:'1234', name: 'J. Smith'};
+
+ // we can create an instance as well
+ var newCard = new CreditCard({number:'0123'});
+ newCard.name = "Mike Smith";
+ newCard.$save();
+ // POST: /user/123/card {number:'0123', name:'Mike Smith'}
+ // server returns: {id:789, number:'01234', name: 'Mike Smith'};
+ expect(newCard.id).toEqual(789);
+ *
+ *
+ *
+ * @param {string} url A parameterized URL template with parameters prefixed by `:` as in
+ * `/user/:username`.
+ * @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in
+ * `actions` methods.
+ * @param {Object.