8000 add customPatch method · sumit-jaiswal/restangular@01297fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 01297fe

Browse files
committed
add customPatch method
1 parent 21b333d commit 01297fe

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ These are the methods that can be called on the Restangular object.
765765
* **customDELETE(path, [params, headers])**: Does a DELETE to the specific path. Optionally you can set params and headers.
766766
* **customPOST([elem, path, params, headers])**: Does a POST to the specific path. Optionally you can set params and headers and elem. Elem is the element to post. If it's not set, it's assumed that it's the element itself from which you're calling this function.
767767
* **customPUT([elem, path, params, headers])**: Does a PUT to the specific path. Optionally you can set params and headers and elem. Elem is the element to post. If it's not set, it's assumed that it's the element itself from which you're calling this function.
768+
* **customPATCH([elem, path, params, headers])**: Does a PATCH to the specific path. Accepts the same arguments as customPUT.
768769
* **customOperation(operation, path, [params, headers, elem])**: This does a custom operation to the path that we specify. This method is actually used from all the others in this subsection. Operation can be one of: get, post, put, remove, head, options, patch, trace
769770
* **addRestangularMethod(name, operation, [path, params, headers, elem])**: This will add a new restangular method to this object with the name `name` to the operation and path specified (or current path otherwise). There's a section on how to do this later.
770771

src/restangular.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,14 @@ restangular.provider('Restangular', function() {
183183
oneUrl: 'oneUrl',
184184
allUrl: 'allUrl',
185185
customPUT: 'customPUT',
186+
customPATCH: 'customPATCH',
186187
customPOST: 'customPOST',
187188
customDELETE: 'customDELETE',
188189
customGET: 'customGET',
189190
customGETLIST: 'customGETLIST',
190191
customOperation: 'customOperation',
191192
doPUT: 'doPU 8000 T',
193+
doPATCH: 'doPATCH',
192194
doPOST: 'doPOST',
193195
doDELETE: 'doDELETE',
194196
doGET: 'doGET',
@@ -922,18 +924,18 @@ restangular.provider('Restangular', function() {
922924

923925
function addCustomOperation(elem) {
924926
elem[config.restangularFields.customOperation] = _.bind(customFunction, elem);
925-
_.each(['put', 'post', 'get', 'delete'], function(oper) {
927+
_.each(['put', 'patch', 'post', 'get', 'delete'], function(oper) {
926928
_.each(['do', 'custom'], function(alias) {
927929
var callOperation = oper === 'delete' ? 'remove' : oper;
928930
var name = alias + oper.toUpperCase();
929931
var callFunction;
930932

931-
if (callOperation !== 'put' && callOperation !== 'post') {
932-
callFunction = customFunction;
933-
} else {
933+
if (/^(post|put|patch)$/.test(oper)) {
934934
callFunction = function(operation, elem, path, params, headers) {
935935
return _.bind(customFunction, this)(operation, path, params, headers, elem);
936936
};
937+
} else {
938+
callFunction = customFunction;
937939
}
938940
elem[name] = _.bind(callFunction, elem, callOperation);
939941
});

test/restangularSpec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,13 @@ describe("Restangular", function() {
620620
$httpBackend.flush();
621621
});
622622

623+
it("customPATCH should work", function() {
624+
var data = { foo: 'bar' };
625+
$httpBackend.expectPATCH('/accounts/hey', data).respond(accountsModel);
626+
restangularAccounts.customPATCH(data, 'hey');
627+
$httpBackend.flush();
628+
});
629+
623630
it("options() should safely return", function() {
624631
restangularAccounts.options().then(function() {
625632
expect(true).toBe(true);

0 commit comments

Comments
 (0)
0