diff --git a/README.md b/README.md index 7fc6aae0..59d60e12 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,9 @@ baseAccounts.getList().then(function (accounts) { firstAccount.put(); editFirstAccount.put(); + // PUT /accounts/123. Save will do POST or PUT accordingly + firstAccount.save(); + // DELETE /accounts/123 We don't have first account anymore :( firstAccount.remove(); @@ -671,6 +674,7 @@ These are the methods that can be called on the Restangular object. * **clone()**: Copies the element * **plain()**: Returns the plain element received from the server without any of the enhanced methods from Restangular. It's an alias to calling `Restangular.stripRestangular(elem)` * **withHttpConfig(httpConfig)**: It lets you set a configuration for $http only for the next call. Check the Local Config HTTP section for an example. +* **save**: Calling save will determine wether to do PUT or POST accordingly ### Collection methods * **getList([queryParams, headers]): Gets itself again (Remember this is a collection)**. diff --git a/src/restangular.js b/src/restangular.js index af52d56c..92b65db4 100644 --- a/src/restangular.js +++ b/src/restangular.js @@ -199,7 +199,8 @@ module.provider('Restangular', function() { withConfig: 'withConfig', withHttpConfig: 'withHttpConfig', singleOne: 'singleOne', - plain: 'plain' + plain: 'plain', + save: 'save' }; object.setRestangularFields = function(resFields) { config.restangularFields = @@ -968,6 +969,7 @@ module.provider('Restangular', function() { localElem[config.restangularFields.trace] = _.bind(traceFunction, localElem); localElem[config.restangularFields.options] = _.bind(optionsFunction, localElem); localElem[config.restangularFields.patch] = _.bind(patchFunction, localElem); + localElem[config.restangularFields.save] = _.bind(save, localElem); addCustomOperation(localElem); return config.transformElem(localElem, false, route, service, true); @@ -1097,6 +1099,14 @@ module.provider('Restangular', function() { return this; } + function save(params, headers) { + if (this[config.restangularFields.fromServer]) { + return this[config.restangularFields.put](params, headers); + } else { + return _.bind(elemFunction, this)("post", undefined, params, undefined, headers); + } + } + function elemFunction(operation, what, params, obj, headers) { var __this = this; var deferred = $q.defer(); diff --git a/test/restangularSpec.js b/test/restangularSpec.js index e20e8901..6dcad6ba 100644 --- a/test/restangularSpec.js +++ b/test/restangularSpec.js @@ -85,6 +85,10 @@ describe("Restangular", function() { return [200, "", ""]; }); + $httpBackend.whenPOST("/accounts/1").respond(function(method, url, data, headers) { + return [200, "", ""]; + }); + $httpBackend.whenPUT("/accounts/1").respond(function(method, url, data, headers) { accountsModel[1] = angular.fromJson(data); return [201, data, ""]; @@ -453,6 +457,24 @@ describe("Restangular", function() { $httpBackend.flush(); }); + it("Should save as put correctly", function() { + restangularAccount1.get().then(function(account) { + $httpBackend.expectPUT('/accounts/1'); + account.put(); + }); + + $httpBackend.flush(); + }); + + it("Should save as post correctly", function() { + var account1 = angular.copy(restangularAccount1); + $httpBackend.expectPOST('/accounts/1'); + account1.name = "Hey"; + account1.save(); + + $httpBackend.flush(); + }); + it("Should make RequestLess connections with one", function() { restangularAccount1.one("transactions", 1).get().then(function(transaction) { expect(Restangular.stripRestangular(transaction))