Skip to content
Open
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
34 changes: 24 additions & 10 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,39 @@ var Events = require('./events'),
save: function () {
var model = this;
if (!model.idAttribute) {
throw new Error('model without declared perisitent id attribute cat not be saved');
throw new Error('model without declared perisitent id attribute can not be saved');
}
return this._rejectDestructed().then(function () {
var promise;
if (model.isNew()) {
return model.ready().then(function () {
promise = model.ready().then(function () {
return model.storage.insert(model);
}).then(function (id) {
model.idAttribute.set(id);
model.commit();
model.calculate();
return model.ready();
}).then(function (data) {
var type = typeof data,
result;
if (type === 'object' && data !== null) {
result = data;
} else {
result = {};
result[model.idAttribute.name] = data;
}
return result;
});

} else {
return model.ready().then(function () {
promise = model.ready().then(function () {
return model.storage.update(model);
}).then(function () {
model.commit();
});
}

return promise.then(function (data) {
if (data) {
model.set(data);
model.commit();
model.calculate().done();
}
return model.ready();
});
});
},

Expand Down
38 changes: 38 additions & 0 deletions test/models/persistent-with-updates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var Model = require('../../lib/model'),
Vow = require('vow'),
storage = [],
prepareData;

prepareData = function (data) {
data.a = data.a + '_';
return data;
};

module.exports = Model.inherit({
attributes: {
id: Model.attributeTypes.Id,
a: Model.attributeTypes.String.inherit({
default: 'a-0'
})
},

storage: Model.Storage.inherit({
insert: function(model) {
return Vow.fulfill().delay(0).then(function () {
var data = model.toJSON();
data.id = storage.length;
storage.push(prepareData(data));
return data;
});
},

update: function(model) {
return Vow.fulfill().delay(0).then(function () {
var data = prepareData(model.toJSON());
storage[model.getId()] = data;
return data;
});
}
})

});
26 changes: 26 additions & 0 deletions test/persistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,30 @@ describe('Persistent', function () {
});
}).done();
});
it('should set data from storage to model on insert', function () {
var PersistenWithUpdates = require('./models/persistent-with-updates'),
model;

model = new PersistenWithUpdates({
a: 'a-1'
});
return model.save().then(function () {
expect(model.get('a')).to.be.equal('a-1_');
});
});
it('should set data from storage to model on update', function () {
var PersistenWithUpdates = require('./models/persistent-with-updates'),
model;

model = new PersistenWithUpdates({
a: 'a-1'
});

return model.save().then(function () {
model.set('a', 'a-2');
return model.save().then(function () {
expect(model.get('a')).to.be.equal('a-2_');
});
});
});
});