Add data source attach example
This commit is contained in:
parent
1dee840de5
commit
a690f8d8df
|
@ -1,4 +1,5 @@
|
||||||
var DataSource = require('../../jugglingdb').DataSource;
|
var DataSource = require('../../jugglingdb').DataSource;
|
||||||
|
var ADL = require('../../jugglingdb').ADL;
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
||||||
// define models
|
// define models
|
||||||
|
@ -91,5 +92,21 @@ Article.create(function(e, article) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// should be able to attach a data source to an existing model
|
||||||
|
var adl = new ADL();
|
||||||
|
|
||||||
|
Color = adl.define('Color', {
|
||||||
|
name: String
|
||||||
|
});
|
||||||
|
|
||||||
|
// attach
|
||||||
|
ds.attach(Color);
|
||||||
|
|
||||||
|
Color.create({name: 'red'});
|
||||||
|
Color.create({name: 'green'});
|
||||||
|
Color.create({name: 'blue'});
|
||||||
|
|
||||||
|
Color.all(function (err, colors) {
|
||||||
|
console.log(colors);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
|
@ -329,7 +329,7 @@ function hiddenProperty(where, property, value) {
|
||||||
Object.defineProperty(where, property, {
|
Object.defineProperty(where, property, {
|
||||||
writable: false,
|
writable: false,
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
configurable: false,
|
configurable: true,
|
||||||
value: value
|
value: value
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,17 +241,29 @@ DataSource.prototype.mixin = function (ModelCtor) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
DataSource.prototype.attach = function (ModelCtor) {
|
DataSource.prototype.attach = function (ModelCtor) {
|
||||||
|
var properties = ModelCtor.schema.definitions[ModelCtor.modelName].properties;
|
||||||
|
var settings = ModelCtor.schema.definitions[ModelCtor.modelName].settings;
|
||||||
|
|
||||||
this.mixin(ModelCtor);
|
this.mixin(ModelCtor);
|
||||||
|
|
||||||
if(this.adapter) {
|
if(this.adapter) {
|
||||||
// pass control to adapter
|
// pass control to adapter
|
||||||
this.adapter.define({
|
this.adapter.define({
|
||||||
model: ModelCtor,
|
model: ModelCtor,
|
||||||
properties: ModelCtor.properties,
|
properties: properties,
|
||||||
settings: ModelCtor.settings
|
settings: settings
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// redefine the schema
|
||||||
|
hiddenProperty(ModelCtor, 'schema', this);
|
||||||
|
|
||||||
|
// add to def
|
||||||
|
this.definitions[ModelCtor.modelName] = {
|
||||||
|
properties: properties,
|
||||||
|
settings: settings
|
||||||
|
};
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,7 @@ ModelBaseClass.prototype._initProperties = function (data, applySetters) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var ctor = this.constructor;
|
var ctor = this.constructor;
|
||||||
var ds = ctor.schema.definitions[ctor.modelName];
|
var ds = ctor.schema.definitions[ctor.modelName];
|
||||||
|
|
||||||
var properties = ds.properties;
|
var properties = ds.properties;
|
||||||
data = data || {};
|
data = data || {};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue