Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Extended customizability of $resource #1045

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,18 @@ angular.module('ngResource', ['ng']).
}

var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
$http({
var config = angular.extend({
method: action.method,
url: route.url(extend({}, extractParams(data), action.params || {}, params)),
data: data
}).then(function(response) {
var data = response.data;
}, (action.httpConfig || {}));
$http(config).then(function(response) {
var data;
if (action.parseResponse) {
data = action.parseResponse(response);
} else {
data = response.data;
}

if (data) {
if (action.isArray) {
Expand Down
17 changes: 17 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ describe("resource", function() {
expect(item).toEqualData({id: 'abc'});
});

it("should let custom action httpConfig and parseResponse override defaults", function() {
$httpBackend.expect('GET', '/Book/234').respond({result: {author: 'Dickens'}});
var Book = $resource(
'/Book/:id',
{id:'@bookId'},
{get: {
method: 'POST',
parseResponse: function(response) { return response.data.result },
httpConfig: { method: 'GET' } //custom httpConfig overrides default
}}
);
var book = Book.get({bookId: 234});

$httpBackend.flush();
expect(book).toEqualData({author: 'Dickens'});
});


it("should create resource", function() {
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123, name: 'misko'});
Expand Down