Add data source attach example
This commit is contained in:
parent
1dee840de5
commit
a690f8d8df
|
@ -1,4 +1,5 @@
|
|||
var DataSource = require('../../jugglingdb').DataSource;
|
||||
var ADL = require('../../jugglingdb').ADL;
|
||||
var ds = new DataSource('memory');
|
||||
|
||||
// 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, {
|
||||
writable: false,
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
configurable: true,
|
||||
value: value
|
||||
});
|
||||
}
|
||||
|
|
|
@ -241,17 +241,29 @@ DataSource.prototype.mixin = 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);
|
||||
|
||||
if(this.adapter) {
|
||||
// pass control to adapter
|
||||
this.adapter.define({
|
||||
model: ModelCtor,
|
||||
properties: ModelCtor.properties,
|
||||
settings: ModelCtor.settings
|
||||
properties: properties,
|
||||
settings: settings
|
||||
});
|
||||
}
|
||||
|
||||
// redefine the schema
|
||||
hiddenProperty(ModelCtor, 'schema', this);
|
||||
|
||||
// add to def
|
||||
this.definitions[ModelCtor.modelName] = {
|
||||
properties: properties,
|
||||
settings: settings
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@ ModelBaseClass.prototype._initProperties = function (data, applySetters) {
|
|||
var self = this;
|
||||
var ctor = this.constructor;
|
||||
var ds = ctor.schema.definitions[ctor.modelName];
|
||||
|
||||
var properties = ds.properties;
|
||||
data = data || {};
|
||||
|
||||
|
|
Loading…
Reference in New Issue