Skip to content

Commit

Permalink
When data is refreshed, the decoded values are marked as 'Changes'. (r…
Browse files Browse the repository at this point in the history
…esolves #384)
  • Loading branch information
ClickerMonkey committed Mar 28, 2018
1 parent fbd4b48 commit a285fcb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
5 changes: 3 additions & 2 deletions build/rekord.js
Original file line number Diff line number Diff line change
Expand Up @@ -4348,14 +4348,15 @@ Class.create( Database,
model.$saved = {};
}

var current = model.$toJSON( true );
var current = model; // model.$toJSON( true );
var conflicts = {};
var conflicted = false;
var updated = {};
var previous = {};
var saved = {};
var notReallySaved = isEmpty( model.$saved );
var relations = db.relations;
var compareTo = db.decode( model.$saved ); // model.$saved

for (var prop in encoded)
{
Expand All @@ -4372,7 +4373,7 @@ Class.create( Database,
}

var currentValue = current[ prop ];
var savedValue = model.$saved[ prop ];
var savedValue = compareTo[ prop ];

previous[ prop ] = model[ prop ];
saved[ prop ] = savedValue;
Expand Down
2 changes: 1 addition & 1 deletion build/rekord.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/rekord.min.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/lib/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,14 +669,15 @@ Class.create( Database,
model.$saved = {};
}

var current = model.$toJSON( true );
var current = model; // model.$toJSON( true );
var conflicts = {};
var conflicted = false;
var updated = {};
var previous = {};
var saved = {};
var notReallySaved = isEmpty( model.$saved );
var relations = db.relations;
var compareTo = db.decode( model.$saved ); // model.$saved

for (var prop in encoded)
{
Expand All @@ -693,7 +694,7 @@ Class.create( Database,
}

var currentValue = current[ prop ];
var savedValue = model.$saved[ prop ];
var savedValue = compareTo[ prop ];

previous[ prop ] = model[ prop ];
saved[ prop ] = savedValue;
Expand Down
40 changes: 40 additions & 0 deletions test/cases/rekord.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,43 @@ test( 'unload', function()

strictEqual( Task1.Database.backend, 'local' );
});

test( 'refresh with decoding', function()
{
var prefix = 'Rekord_refresh_decoding_';
var TaskName = prefix + 'task';

var Task = Rekord({
name: TaskName,
fields: ['done', 'name'],
decodings: {
done: function(value) {
return /^(yes|y|ya|1|t|true)$/i.test(value + '');
}
}
});

var t1 = Task.boot({id: 1, done: 'y', name: 't1'});

notOk( t1.$hasChanges() );
notOk( t1.$hasChange('done') );
strictEqual( t1.done, true );

t1.$remote({done: 'f'});

notOk( t1.$hasChanges() );
notOk( t1.$hasChange('done') );
strictEqual( t1.done, false );

t1.$remote({done: 't'}, true);

notOk( t1.$hasChanges() );
notOk( t1.$hasChange('done') );
strictEqual( t1.done, true );

Task.Database.live.liveSave({id: 1, done: 'no'});

notOk( t1.$hasChanges() );
notOk( t1.$hasChange('done') );
strictEqual( t1.done, false );
});

0 comments on commit a285fcb

Please sign in to comment.