Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var boot = require('loopback-boot');
var started = new Date();
var env = app.get('env');

app.set('legacyExplorer', false);

// required to support base models
app.dataSource('db', {
connector: loopback.Memory,
Expand Down
10 changes: 6 additions & 4 deletions models/model-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var ModelDefinition = app.models.ModelDefinition;

/**
* - `name` is required and must be unique per `Facet`
*
*
* @header Property Validation
*/

Expand Down Expand Up @@ -140,8 +140,10 @@ function cleanRelatedData(relatedData, relation) {
}
}

ModelDefinition.afterCreate = function(next) {
var def = this;
ModelDefinition.observe("after save", function(ctx, next) {
if (!ctx.isNewInstance) return next();

var def = ctx.instance;
var scriptPath = def.getScriptPath();

fs.exists(scriptPath, function(exists) {
Expand All @@ -151,7 +153,7 @@ ModelDefinition.afterCreate = function(next) {
createScript(def, scriptPath, next);
}
});
}
});

ModelDefinition.prototype.getClassName = function() {
if(!this.name) return null;
Expand Down
20 changes: 10 additions & 10 deletions models/workspace-entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ WorkspaceEntity.getUniqueIdParts = function(data) {
return [];
}
}

if(name) parts.push(name);

return parts;
Expand Down Expand Up @@ -162,10 +162,10 @@ WorkspaceEntity.getDataFromConfig = function(config) {
// Automatically inject parent model's facetName when creating a new object
// We have to perform this task before the validations are executed, since
// the `facetName` is a required property
WorkspaceEntity.beforeValidate = function injectFacetName(next) {
var Entity = this.constructor;
WorkspaceEntity.observe('before save', function injectFacetName(ctx, next) {
var Entity = ctx.Model;
var properties = Entity.definition.properties;
var data = this.toObject();
var data = ctx.instance ? ctx.instance.toObject() : ctx.data;

if (!('facetName' in properties &&
'modelId' in properties &&
Expand All @@ -175,17 +175,17 @@ WorkspaceEntity.beforeValidate = function injectFacetName(next) {

Entity.app.models.ModelDefinition.findById(data.modelId, function(err, model) {
if (model && model.facetName) {
if (this.facetName && this.facetName !== model.facetName) {
if (data.facetName && data.facetName !== model.facetName) {
console.warn(
'Warning: fixed %s[%s].facetName from %j to %j' +
' to match the parent',
Entity.modelName,
this.id,
this.facetName,
data.id,
data.facetName,
model.facetName);
}
this.facetName = model.facetName;
(ctx.instance || ctx.data).facetName = model.facetName;
}
next();
}.bind(this));
};
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"lodash": "~2.4.1",
"loopback": "^2.0.0",
"loopback-boot": "^1.0.0",
"loopback-datasource-juggler": "^2.6.1",
"loopback-datasource-juggler": "^2.22.0",
"method-override": "^2.1.1",
"morgan": "^1.2.0",
"ncp": "^2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions test/model-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ describe('ModelDefinition', function() {
it('includes `name` property', function(done) {
new TestDataBuilder()
.define('model', ModelDefinition, {
facetName: 'server',
name: 'test-model'
})
.buildTo(this, function(err) {
Expand Down Expand Up @@ -198,6 +199,7 @@ describe('ModelDefinition', function() {
it('includes all custom properties', function(done) {
new TestDataBuilder()
.define('model', ModelDefinition, {
facetName: 'server',
name: 'test-model',
custom: 'custom'
})
Expand Down