by @PeterPeerdeman from
PUT /customers/1
{
'id':1,
'name':'Peter',
'shoesize': 46
}
PATCH /customers/1
{
'name':'Peter'
}
var Customer = $resource('/api/v1/customers/:customerId', {customerId:'@id'});
//performs GET /api/v1/customers/123
$scope.customer = Customer.get({customerId:123}, function() {
//callback
});
var Establishment = $resource('/api/v1/customers/:customerId/ ' +
'establishments/:establishmentId', {customerId:123, establishmentId:'@id'});
//performs GET /api/v1/customers/123/establishments/
var establishments = Establishment.query(function() {
var establishment = _.find(establishments, {city:'Amsterdam'});
establishment.name = "Hippo HQ";
establishment.$save();
});
RestangularProvider.setBaseUrl('/api/v1')
//performs GET /api/v1/customers/123
Restangular.one('customers', 123).get().then(function(customer) {
$scope.customer = customer
});
//performs GET /api/v1/customers/123/establishments
$scope.customer.getList('establishments').then(function(establishments) {
var establishment = _.find(establishments, {city:'The Hague'});
establishment.name = "Q42";
establishment.put();
});
var newEstablishment = {name:'Lifely', address:'zekeringstraat 17'};
//performs POST /api/v1/customers/123/establishments
$scope.customer.getList('establishments').post(newCustomer).then(function(data) {
newCustomer.id = data.id;
$scope.customer.establishments.push(newEstablishment);
})
//performs POST /api/v1/customers/favorite
$scope.customer.customPost({}, 'favorite');
//e.g. our services return
//single resource {user:{name:'peter'}}
//list resource {data:[{name:'peter'}]
Restangular.setResponseInterceptor(function(response, operation, what) {
var newResponse;
if (operation === "getList") {
// This is a wrapped array
newResponse = response.data;
} else {
// This is a wrapped element
newResponse = response[what];
}
return newResponse;
});
RestangularProvider.setRequestInterceptor(
function(elem, operation, what) {
if (operation === 'put') {
elem._id = undefined;
return elem;
}
return elem;
});
Restangular
by @PeterPeerdeman from