Add data source attach example

This commit is contained in:
Ritchie 2013-05-23 17:29:03 -07:00
parent 1dee840de5
commit a690f8d8df
4 changed files with 37 additions and 7 deletions

View File

@ -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);
});

View File

@ -329,7 +329,7 @@ function hiddenProperty(where, property, value) {
Object.defineProperty(where, property, {
writable: false,
enumerable: false,
configurable: false,
configurable: true,
value: value
});
}

View File

@ -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
});
// pass control to adapter
this.adapter.define({
model: ModelCtor,
properties: properties,
settings: settings
});
}
// redefine the schema
hiddenProperty(ModelCtor, 'schema', this);
// add to def
this.definitions[ModelCtor.modelName] = {
properties: properties,
settings: settings
};
return this;
}

View File

@ -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 || {};