http://docs.angularjs.org/api/ngResource.$resource
In the provided link, an example is given as follows:
// Defining a CreditCard class
var CreditCard = $resource('/user/:userId/card/:cardId',
{userId:123, cardId:'@id'}, {
charge: {method:'POST', params:{charge:true}}
});
// Fetching a collection from the server
var cards = CreditCard.query(function() {
// GET: /user/123/card
// Server response: [ {id:456, number:'1234', name:'Smith'} ];
var card = cards[0];
// Each item represents an instance of CreditCard
expect(card instanceof CreditCard).toEqual(true);
card.name = "J. Smith";
// Non-GET methods are applied to the instances
card.$save();
// POST: /user/123/card/456 {id:456, number:'1234', name:'J. Smith'}
// Server response: {id:456, number:'1234', name: 'J. Smith'};
// Custom method also functions correctly.
card.$charge({amount:9.99});
// POST: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'J. Smith'}
});
// Creating a new instance
var newCard = new CreditCard({number:'0123'});
newCard.name = "Mike Smith";
newCard.$save();
// POST: /user/123/card {number:'0123', name:'Mike Smith'}
// Server response: {id:789, number:'01234', name: 'Mike Smith'};
expect(newCard.id).toEqual(789);
In the code snippet, you will find this line:
var card = cards[0];
I am unsure about the origin of the array
cards
. It seems to be referenced at the previous line, but it appears out of the function scope.Regarding the jasmine expect function, I question if Angular executes it and generates any errors?
Lines featuring the expect()
function include:
expect(card instanceof CreditCard).toEqual(true);
This function is typically used for Jasmine testing; however, there is no direct indication of Jasmine being present in the browser/Angular environment mentioned.