1
0
Fork 0

Foxx Repository: Save, Replace, ReplaceById return the model

For convenience, this is quite useful in some cases
This commit is contained in:
Lucas Dohmen 2013-09-17 14:37:50 +02:00
parent fba6c94671
commit 43988bb3ae
2 changed files with 9 additions and 3 deletions

View File

@ -123,7 +123,7 @@ function RepositoryMethodsSpec() {
.withArguments(modelData)
.andReturn(id_and_rev);
instance.save(model);
assertEqual(instance.save(model), model);
model.assertIsSatisfied();
collection.assertIsSatisfied();
@ -167,7 +167,7 @@ function RepositoryMethodsSpec() {
.withArguments(id, data)
.andReturn(id_and_rev);
instance.replace(model);
assertEqual(instance.replace(model), model);
collection.assertIsSatisfied();
model.assertIsSatisfied();
@ -187,7 +187,7 @@ function RepositoryMethodsSpec() {
.withArguments(id, data)
.andReturn(id_and_rev);
instance.replaceById(id, model);
assertEqual(instance.replaceById(id, model), model);
collection.assertIsSatisfied();
model.assertIsSatisfied();

View File

@ -105,11 +105,13 @@ _.extend(Repository.prototype, {
/// @brief Save a model into the database
///
/// Expects a model. Will set the ID and Rev on the model.
/// Returns the model (for convenience).
////////////////////////////////////////////////////////////////////////////////
save: function (model) {
'use strict';
var id_and_rev = this.collection.save(model.forDB());
model.set(id_and_rev);
return model;
},
////////////////////////////////////////////////////////////////////////////////
@ -140,6 +142,7 @@ _.extend(Repository.prototype, {
///
/// Find the model in the database by its `_id` and replace it with this version.
/// Expects a model. Sets the Revision of the model.
/// Returns the model (for convenience).
////////////////////////////////////////////////////////////////////////////////
replace: function (model) {
'use strict';
@ -147,6 +150,7 @@ _.extend(Repository.prototype, {
data = model.forDB(),
id_and_rev = this.collection.replace(id, data);
model.set(id_and_rev);
return model;
},
////////////////////////////////////////////////////////////////////////////////
@ -156,12 +160,14 @@ _.extend(Repository.prototype, {
/// Find the model in the database by the given ID and replace it with the given.
/// model.
/// Expects a model. Sets the ID and Revision of the model.
/// Returns the model (for convenience).
////////////////////////////////////////////////////////////////////////////////
replaceById: function (id, model) {
'use strict';
var data = model.forDB(),
id_and_rev = this.collection.replace(id, data);
model.set(id_and_rev);
return model;
},
////////////////////////////////////////////////////////////////////////////////