Skip to content

Commit

Permalink
Added save method
Browse files Browse the repository at this point in the history
Fixes #584
  • Loading branch information
mgonto committed Apr 25, 2014
1 parent 3edf585 commit 1882f90
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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)**.
Expand Down
12 changes: 11 additions & 1 deletion src/restangular.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 22 additions & 0 deletions test/restangularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, ""];
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 1882f90

Please sign in to comment.