Allows unknown properties to be saved for non-strict models

See https://github.com/strongloop/loopback/issues/199
This commit is contained in:
Raymond Feng 2014-02-24 18:38:45 -08:00
parent 4765fbed62
commit 44a62d01af
2 changed files with 28 additions and 2 deletions

View File

@ -876,8 +876,9 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb
var typedData = {}; var typedData = {};
for (var key in data) { for (var key in data) {
// Convert the properties by type
inst[key] = data[key]; inst[key] = data[key];
typedData[key] = inst.__data[key]; typedData[key] = inst[key];
} }
inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst), inst.constructor._forDB(typedData), function (err) { inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst), inst.constructor._forDB(typedData), function (err) {
@ -886,7 +887,6 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb
for (var key in data) { for (var key in data) {
inst.__dataWas[key] = inst.__data[key]; inst.__dataWas[key] = inst.__data[key];
} }
;
} }
done.call(inst, function () { done.call(inst, function () {
saveDone.call(inst, function () { saveDone.call(inst, function () {

View File

@ -486,6 +486,32 @@ describe('DataSource define model', function () {
}); });
it('should update the instance with unknown properties', function (done) {
var ds = new DataSource('memory');// define models
Post = ds.define('Post', {
title: { type: String, length: 255, index: true },
content: { type: String }
});
Post.create({title: 'a', content: 'AAA'}, function (err, post) {
post.updateAttributes({title: 'b', xyz: 'xyz'}, function (err, p) {
should.not.exist(err);
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.xyz.should.be.equal('xyz');
Post.findById(post.id, function (err, p) {
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.xyz.should.be.equal('xyz');
p.title.should.be.equal('b');
done();
});
});
});
});
it('injects id by default', function (done) { it('injects id by default', function (done) {
var ds = new ModelBuilder(); var ds = new ModelBuilder();