Keep dirty state for cached objects

This commit is contained in:
Anatoliy Chakkaev 2011-11-23 12:38:04 +07:00
parent 12ea5f8478
commit 8b720c30f8
2 changed files with 16 additions and 1 deletions

View File

@ -181,6 +181,12 @@ AbstractClass.all = function all(params, cb) {
// TODO: think about better implementation, test keeping dirty state // TODO: think about better implementation, test keeping dirty state
if (constr.cache[d.id]) { if (constr.cache[d.id]) {
obj = constr.cache[d.id]; obj = constr.cache[d.id];
// keep dirty attributes untouthed (remove from dataset)
Object.keys(obj.toObject()).forEach(function (attr) {
if (attr in d && obj.propertyChanged(attr)) {
delete d[attr];
}
});
constr.call(obj, d); constr.call(obj, d);
} else { } else {
obj = new constr(d); obj = new constr(d);

View File

@ -272,5 +272,14 @@ it 'should validate uniqueness', (test) ->
user.email = 'unique@email.tld' user.email = 'unique@email.tld'
user.isValid (valid) -> user.isValid (valid) ->
test.ok valid, 'valid with unique email' test.ok valid, 'valid with unique email'
user.save ->
test.done() test.done()
it 'should save dirty state when validating uniqueness', (test) ->
User.all where: email: 'unique@email.tld' , (err, users) ->
u = users[0]
u.name = 'Hulk'
u.isValid (valid) ->
test.ok valid
test.equal u.name, 'Hulk'
test.done()