Created
February 14, 2014 08:16
-
-
Save sebald/8997503 to your computer and use it in GitHub Desktop.
Angular: Transform Resources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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