Created
November 25, 2013 10:33
-
-
Save dburles/7639410 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Factory = function() { | |
var factoryName; | |
var factories = {}; | |
this.define = function(name, collection, attributes) { | |
factoryName = name; | |
factories[name] = { | |
name: name, | |
collection: collection, | |
attributes: attributes, | |
has: [] | |
}; | |
return this; | |
}; | |
this.has = function(name) { | |
console.log(factoryName + ' has ' + name); | |
factories[factoryName].has.push(name); | |
return this; | |
}; | |
this.build = function(name) { | |
console.log('build: ' + name); | |
var insertId = this._insert(name); | |
return factories[name].collection.findOne(insertId); | |
}; | |
this._insert = function(name) { | |
var self = this; | |
var factory = factories[name]; | |
var insertId = factory.collection.insert(factory.attributes); | |
_.each(factory.has, function(n) { | |
var update = {}; | |
var relId = self._insert(n); | |
update[n + 'Id'] = relId; | |
factory.collection.update(insertId, { $set: update }); | |
}); | |
return insertId; | |
}; | |
this.debug = function() { | |
console.log('factories: ', factories); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment