Skip to content

Instantly share code, notes, and snippets.

@sebald
Created February 14, 2014 08:16
Show Gist options
  • Select an option

  • Save sebald/8997503 to your computer and use it in GitHub Desktop.

Select an option

Save sebald/8997503 to your computer and use it in GitHub Desktop.
Angular: Transform Resources
$httpBackend.expect('GET', '/Customer/1', null).respond({
id: 1,
name: 'John Doe',
age: 42,
purchases: [
{
id: 1,
items: [
{ name: 'Shampoo' },
{ name: 'Soap' }
]
}
]
});
function Purchase ( p ) {
this.id = p.id;
this.items = p.items;
}
Purchase.prototype.addItem = function ( item ) {
this.items.add({ name: 'Beer' });
};
var transform = function(response) {
var model = response.model;
angular.forEach( model.purchases , function ( purchase, i ) {
model.purchases[i] = new Purchase( purchase );
});
model.hello = function () { return 'hello'; };
return model;
};
var model = $model('/Customer/:id', {}, {
get: {
method: 'get',
interceptor: {
response: transform
}
}
}),
customer;
customer = model.get({ id: 1 });
$httpBackend.flush();
console.log(customer.purchases[0].addItem);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment