Skip to content

Commit

Permalink
Version 1.5.10
Browse files Browse the repository at this point in the history
Added load priority
  • Loading branch information
ClickerMonkey committed Apr 7, 2018
1 parent 9e6aca5 commit 6663dbe
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 44 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rekord",
"version": "1.5.9",
"version": "1.5.10",
"homepage": "https://github.com/Rekord/rekord",
"authors": [
"Philip Diffenderfer <[email protected]>"
Expand Down
103 changes: 85 additions & 18 deletions build/rekord.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* rekord 1.5.9 - A javascript REST ORM that is offline and real-time capable http://rekord.github.io/rekord/ by Philip Diffenderfer */
/* rekord 1.5.10 - A javascript REST ORM that is offline and real-time capable http://rekord.github.io/rekord/ by Philip Diffenderfer */
// UMD (Universal Module Definition)
(function (root, factory)
{
Expand Down Expand Up @@ -2666,6 +2666,13 @@ Rekord.load = function(callback, context)
}
}

// Load by priority defined in Database
loading.sort(function(a, b)
{
return b.priority - a.priority;
});

// Begin the loading procedure for every unloaded Database
for (var i = 0; i < loading.length; i++)
{
loading[ i ].loadBegin( onLoadFinish );
Expand Down Expand Up @@ -3878,6 +3885,7 @@ var Defaults = Database.Defaults =
defaults: {},
publishAlways: [],
saveAlways: [],
priority: 0,
comparator: null,
comparatorNullsFirst: null,
revision: null,
Expand Down Expand Up @@ -4698,7 +4706,13 @@ Class.create( Database,
var encoded = records[ i ];
var key = keys[ i ];
var decoded = db.decode( copy( encoded, true ) );
var model = db.instantiate( decoded, true );
var existing = db.all[ key ];
var model = existing || db.instantiate( decoded, true );

if (existing)
{
model.$set( decoded, undefined, true );
}

if ( model.$invalid === true )
{
Expand Down Expand Up @@ -5211,6 +5225,11 @@ Class.create( Model,
this.$reset( props );
}

this.$initRelations( remoteData );
},

$initRelations: function(remoteData)
{
if ( this.$db.loadRelations )
{
var databaseRelations = this.$db.relations;
Expand Down Expand Up @@ -13808,11 +13827,16 @@ Class.create( Relation,
* @param {Rekord.Model} model [description]
*/

load: Gate(function(model, initialValue, remoteData)
load: Gate(function(model, initialValue, remoteData, skipInitial)
{

}),

setInitial: function(model, initialValue, remoteData)
{

},

set: function(model, input, remoteData)
{

Expand Down Expand Up @@ -14746,6 +14770,13 @@ Class.extend( RelationSingle, BelongsTo,
model.$on( Model.Events.PostRemove, this.postRemove, this );
model.$on( Model.Events.KeyUpdate, this.onKeyUpdate, this );

this.setInitial( model, initialValue, remoteData );
}),

setInitial: function(model, initialValue, remoteData)
{
var relation = model.$relations[ this.name ];

if ( isEmpty( initialValue ) )
{
initialValue = this.grabInitial( model, this.local );
Expand All @@ -14766,7 +14797,7 @@ Class.extend( RelationSingle, BelongsTo,
{
relation.query = this.executeQuery( model );
}
}),
},

sync: function(model, removeUnrelated)
{
Expand Down Expand Up @@ -14891,6 +14922,13 @@ Class.extend( RelationSingle, HasOne,
model.$on( Model.Events.PreSave, this.preSave, this );
model.$on( Model.Events.PostRemove, this.postRemove, this );

this.setInitial( model, initialValue, remoteData );
}),

setInitial: function(model, initialValue, remoteData)
{
var relation = model.$relations[ this.name ];

if ( isEmpty( initialValue ) )
{
initialValue = this.grabInitial( model, this.local );
Expand All @@ -14912,7 +14950,7 @@ Class.extend( RelationSingle, HasOne,
{
relation.query = this.executeQuery( model );
}
}),
},

populateInitial: function(initialValue, relation, model)
{
Expand Down Expand Up @@ -15165,6 +15203,16 @@ Class.extend( RelationMultiple, HasMany,
}

// If the model's initial value is an array, populate the relation from it!
this.setInitial( model, initialValue, remoteData );

// We only need to set the property once since the underlying array won't change.
this.setProperty( relation );
}),

setInitial: function(model, initialValue, remoteData)
{
var relation = model.$relations[ this.name ];

if ( isArray( initialValue ) )
{
Rekord.debug( Rekord.Debugs.HASMANY_INITIAL, this, model, relation, initialValue );
Expand All @@ -15181,10 +15229,7 @@ Class.extend( RelationMultiple, HasMany,

this.ready( this.handleLazyLoad( relation ) );
}

// We only need to set the property once since the underlying array won't change.
this.setProperty( relation );
}),
},

sync: function(model, removeUnrelated)
{
Expand Down Expand Up @@ -15608,6 +15653,17 @@ Class.extend( RelationMultiple, HasManyThrough,
}

// If the model's initial value is an array, populate the relation from it!
this.setInitial( model, initialValue, remoteData );

// We only need to set the property once since the underlying array won't change.
this.setProperty( relation );
}),

setInitial: function(model, initialValue, remoteData)
{
var relation = model.$relations[ this.name ];
var throughDatabase = this.through.Database;

if ( isArray( initialValue ) )
{
Rekord.debug( Rekord.Debugs.HASMANYTHRU_INITIAL, this, model, relation, initialValue );
Expand All @@ -15624,10 +15680,7 @@ Class.extend( RelationMultiple, HasManyThrough,

throughDatabase.ready( this.handleLazyLoad( relation ), this );
}

// We only need to set the property once since the underlying array won't change.
this.setProperty( relation );
}),
},

sync: function(model, removeUnrelated)
{
Expand Down Expand Up @@ -16287,16 +16340,23 @@ Class.extend( RelationMultiple, HasList,
};

// If the model's initial value is an array, populate the relation from it!
this.setInitial( model, initialValue, remoteData );

// We only need to set the property once since the underlying array won't change.
this.setProperty( relation );
}),

setInitial: function(model, initialValue, remoteData)
{
var relation = model.$relations[ this.name ];

if ( isArray( initialValue ) )
{
Rekord.debug( Rekord.Debugs.HASLIST_INITIAL, this, model, relation, initialValue );

this.grabModels( relation, initialValue, this.handleModel( relation, remoteData ), remoteData );
}

// We only need to set the property once since the underlying array won't change.
this.setProperty( relation );
}),
},

addModel: function(relation, related, remoteData)
{
Expand Down Expand Up @@ -16428,6 +16488,13 @@ Class.extend( RelationSingle, HasReference,
}
};

this.setInitial( model, initialValue, remoteData );
}),

setInitial: function(model, initialValue, remoteData)
{
var relation = model.$relations[ this.name ];

if ( !isEmpty( initialValue ) )
{
Rekord.debug( Rekord.Debugs.HASREFERENCE_INITIAL, this, model, initialValue );
Expand All @@ -16438,7 +16505,7 @@ Class.extend( RelationSingle, HasReference,
{
relation.query = this.executeQuery( model );
}
}),
},

preClone: function(model, clone, properties)
{
Expand Down
12 changes: 6 additions & 6 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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rekord",
"version": "1.5.9",
"version": "1.5.10",
"description": "A javascript REST ORM that is offline and real-time capable http://rekord.github.io/rekord/",
"author": "Philip Diffenderfer",
"license": "MIT",
Expand Down
9 changes: 8 additions & 1 deletion src/lib/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ var Defaults = Database.Defaults =
defaults: {},
publishAlways: [],
saveAlways: [],
priority: 0,
comparator: null,
comparatorNullsFirst: null,
revision: null,
Expand Down Expand Up @@ -1019,7 +1020,13 @@ Class.create( Database,
var encoded = records[ i ];
var key = keys[ i ];
var decoded = db.decode( copy( encoded, true ) );
var model = db.instantiate( decoded, true );
var existing = db.all[ key ];
var model = existing || db.instantiate( decoded, true );

if (existing)
{
model.$set( decoded, undefined, true );
}

if ( model.$invalid === true )
{
Expand Down
5 changes: 5 additions & 0 deletions src/lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ Class.create( Model,
this.$reset( props );
}

this.$initRelations( remoteData );
},

$initRelations: function(remoteData)
{
if ( this.$db.loadRelations )
{
var databaseRelations = this.$db.relations;
Expand Down
7 changes: 7 additions & 0 deletions src/lib/Rekord.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ Rekord.load = function(callback, context)
}
}

// Load by priority defined in Database
loading.sort(function(a, b)
{
return b.priority - a.priority;
});

// Begin the loading procedure for every unloaded Database
for (var i = 0; i < loading.length; i++)
{
loading[ i ].loadBegin( onLoadFinish );
Expand Down
9 changes: 8 additions & 1 deletion src/lib/relations/BelongsTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ Class.extend( RelationSingle, BelongsTo,
model.$on( Model.Events.PostRemove, this.postRemove, this );
model.$on( Model.Events.KeyUpdate, this.onKeyUpdate, this );

this.setInitial( model, initialValue, remoteData );
}),

setInitial: function(model, initialValue, remoteData)
{
var relation = model.$relations[ this.name ];

if ( isEmpty( initialValue ) )
{
initialValue = this.grabInitial( model, this.local );
Expand All @@ -96,7 +103,7 @@ Class.extend( RelationSingle, BelongsTo,
{
relation.query = this.executeQuery( model );
}
}),
},

sync: function(model, removeUnrelated)
{
Expand Down
15 changes: 11 additions & 4 deletions src/lib/relations/HasList.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,23 @@ Class.extend( RelationMultiple, HasList,
};

// If the model's initial value is an array, populate the relation from it!
this.setInitial( model, initialValue, remoteData );

// We only need to set the property once since the underlying array won't change.
this.setProperty( relation );
}),

setInitial: function(model, initialValue, remoteData)
{
var relation = model.$relations[ this.name ];

if ( isArray( initialValue ) )
{
Rekord.debug( Rekord.Debugs.HASLIST_INITIAL, this, model, relation, initialValue );

this.grabModels( relation, initialValue, this.handleModel( relation, remoteData ), remoteData );
}

// We only need to set the property once since the underlying array won't change.
this.setProperty( relation );
}),
},

addModel: function(relation, related, remoteData)
{
Expand Down
15 changes: 11 additions & 4 deletions src/lib/relations/HasMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ Class.extend( RelationMultiple, HasMany,
}

// If the model's initial value is an array, populate the relation from it!
this.setInitial( model, initialValue, remoteData );

// We only need to set the property once since the underlying array won't change.
this.setProperty( relation );
}),

setInitial: function(model, initialValue, remoteData)
{
var relation = model.$relations[ this.name ];

if ( isArray( initialValue ) )
{
Rekord.debug( Rekord.Debugs.HASMANY_INITIAL, this, model, relation, initialValue );
Expand All @@ -143,10 +153,7 @@ Class.extend( RelationMultiple, HasMany,

this.ready( this.handleLazyLoad( relation ) );
}

// We only need to set the property once since the underlying array won't change.
this.setProperty( relation );
}),
},

sync: function(model, removeUnrelated)
{
Expand Down
Loading

0 comments on commit 6663dbe

Please sign in to comment.