Skip to content

Commit

Permalink
Add model method which puts remoteData (resolves #373)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClickerMonkey committed Feb 16, 2017
1 parent c38553c commit 66d5535
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 40 deletions.
37 changes: 25 additions & 12 deletions build/rekord.js
Original file line number Diff line number Diff line change
Expand Up @@ -3695,19 +3695,24 @@ function defaultEncode(model, data, forSaving)
return data;
}

function defaultDecode(rawData)
function defaultDecode(rawData, data)
{
var decodings = this.decodings;
var target = data || rawData;

for (var prop in rawData)
{
if ( prop in decodings )
{
rawData[ prop ] = decodings[ prop ]( rawData[ prop ], rawData, prop );
target[ prop ] = decodings[ prop ]( rawData[ prop ], rawData, prop );
}
else
{
target[ prop ] = rawData[ prop ];
}
}

return rawData;
return target;
}

function defaultSummarize(model)
Expand Down Expand Up @@ -5670,6 +5675,11 @@ Class.create( Model,
return newKey;
},

$remote: function(encoded, overwrite)
{
this.$db.putRemoteData( encoded, this.$key(), this, overwrite );
},

$isSynced: function()
{
return this.$status === Model.Status.Synced;
Expand Down Expand Up @@ -5720,11 +5730,12 @@ Class.create( Model,
return projection.project( this );
},

$getChanges: function(alreadyEncoded)
$getChanges: function(alreadyDecoded)
{
var saved = this.$saved;
var encoded = alreadyEncoded || this.$toJSON( true );
var fields = this.$db.saveFields;
var db = this.$db;
var saved = db.decode( this.$saved, {} );
var encoded = alreadyDecoded || this;
var fields = db.saveFields;

return saved ? diff( encoded, saved, fields, equals ) : encoded;
},
Expand All @@ -5736,13 +5747,15 @@ Class.create( Model,
return true;
}

var ignore = this.$db.ignoredFields;
var encoded = this.$toJSON( true );
var saved = this.$saved;
var db = this.$db;
var ignore = db.ignoredFields;
var saved = db.decode( this.$saved, {} );
var fields = db.saveFields;

for (var prop in encoded)
for (var i = 0; i < fields.length; i++)
{
var currentValue = encoded[ prop ];
var prop = fields[ i ];
var currentValue = this[ prop ];
var savedValue = saved[ prop ];

if ( ignore[ prop ] )
Expand Down
10 changes: 5 additions & 5 deletions 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.

11 changes: 8 additions & 3 deletions src/lib/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,24 @@ function defaultEncode(model, data, forSaving)
return data;
}

function defaultDecode(rawData)
function defaultDecode(rawData, data)
{
var decodings = this.decodings;
var target = data || rawData;

for (var prop in rawData)
{
if ( prop in decodings )
{
rawData[ prop ] = decodings[ prop ]( rawData[ prop ], rawData, prop );
target[ prop ] = decodings[ prop ]( rawData[ prop ], rawData, prop );
}
else
{
target[ prop ] = rawData[ prop ];
}
}

return rawData;
return target;
}

function defaultSummarize(model)
Expand Down
26 changes: 17 additions & 9 deletions src/lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,11 @@ Class.create( Model,
return newKey;
},

$remote: function(encoded, overwrite)
{
this.$db.putRemoteData( encoded, this.$key(), this, overwrite );
},

$isSynced: function()
{
return this.$status === Model.Status.Synced;
Expand Down Expand Up @@ -777,11 +782,12 @@ Class.create( Model,
return projection.project( this );
},

$getChanges: function(alreadyEncoded)
$getChanges: function(alreadyDecoded)
{
var saved = this.$saved;
var encoded = alreadyEncoded || this.$toJSON( true );
var fields = this.$db.saveFields;
var db = this.$db;
var saved = db.decode( this.$saved, {} );
var encoded = alreadyDecoded || this;
var fields = db.saveFields;

return saved ? diff( encoded, saved, fields, equals ) : encoded;
},
Expand All @@ -793,13 +799,15 @@ Class.create( Model,
return true;
}

var ignore = this.$db.ignoredFields;
var encoded = this.$toJSON( true );
var saved = this.$saved;
var db = this.$db;
var ignore = db.ignoredFields;
var saved = db.decode( this.$saved, {} );
var fields = db.saveFields;

for (var prop in encoded)
for (var i = 0; i < fields.length; i++)
{
var currentValue = encoded[ prop ];
var prop = fields[ i ];
var currentValue = this[ prop ];
var savedValue = saved[ prop ];

if ( ignore[ prop ] )
Expand Down
6 changes: 3 additions & 3 deletions test/cases/rekord-belongsto-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ test( 'auto true', function(assert)
var u0 = User.create({name: 'You'});
var t0 = Task.create({name: 'This'});

deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, created_by: undefined} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name} );

t0.$set('creator', u0);

Expand Down Expand Up @@ -408,12 +408,12 @@ test( 'auto false', function(assert)
var u0 = User.create({name: 'You'});
var t0 = Task.create({name: 'This'});

deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, created_by: undefined} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name} );

t0.$set('creator', u0);

notDeepEqual( remote.lastRecord, {created_by: u0.id} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, created_by: undefined} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name} );
strictEqual( t0.created_by, u0.id );
});

Expand Down
6 changes: 3 additions & 3 deletions test/cases/rekord-hasmany-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ test( 'auto true', function(assert)
strictEqual( t0.list, undefined );
strictEqual( l0.tasks.length, 0 );

deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, done: false, task_list_id: undefined} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, done: false} );

l0.tasks.relate( t0 );

Expand Down Expand Up @@ -517,14 +517,14 @@ test( 'auto false', function(assert)
strictEqual( t0.list, undefined );
strictEqual( l0.tasks.length, 0 );

deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, done: false, task_list_id: undefined} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, done: false} );

l0.tasks.relate( t0 );

strictEqual( t0.list, l0 );
strictEqual( l0.tasks.length, 1 );

deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, done: false, task_list_id: undefined} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, done: false} );
});

test( 'property true', function(assert)
Expand Down
6 changes: 3 additions & 3 deletions test/cases/rekord-hasone-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ test( 'auto true', function(assert)
var u0 = User.create({name: 'You'});
var t0 = Task.create({name: 'This'});

deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, created_by: undefined} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name} );

t0.$set('creator', u0);

Expand Down Expand Up @@ -408,12 +408,12 @@ test( 'auto false', function(assert)
var u0 = User.create({name: 'You'});
var t0 = Task.create({name: 'This'});

deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, created_by: undefined} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name} );

t0.$set('creator', u0);

notDeepEqual( remote.lastRecord, {created_by: u0.id} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name, created_by: undefined} );
deepEqual( remote.lastRecord, {id: t0.id, name: t0.name} );
strictEqual( t0.created_by, u0.id );
});

Expand Down
2 changes: 1 addition & 1 deletion test/cases/rekord-hasone.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ test( 'child table default rekord', function(assert)
ok( i0.$isSaved() );
ok( p0.$isSaved() );
deepEqual( i0.$saved, {id: i0.id, name: 'i0'} );
deepEqual( p0.$saved, {item_id: i0.id, brand: undefined, model: undefined} );
deepEqual( p0.$saved, {item_id: i0.id} );
});

test( 'child table default function', function(assert)
Expand Down
30 changes: 30 additions & 0 deletions test/cases/rekordmodel-instance-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,36 @@ test( 'constructor', function(assert)
strictEqual( n2.name, 'name2' );
});

test( '$remote', function(assert)
{
var Task = Rekord({
name: 'Model_remote',
fields: ['name', 'done'],
decodings: {
done: function(x) {
return /(1|true|y|yes)/.test( (x + '').toLowerCase() );
}
}
});

var t0 = new Task.boot({name: 'Phil', done: '1'});

strictEqual( t0.name, 'Phil' );
strictEqual( t0.done, true );

ok( t0.$isSaved() );
notOk( t0.$hasChanges() );

t0.$remote({
done: 'no'
});

strictEqual( t0.done, false );

ok( t0.$isSaved() );
notOk( t0.$hasChanges() );
});

test( '$reset', function(assert)
{
var Todo = Rekord({
Expand Down

0 comments on commit 66d5535

Please sign in to comment.