Skip to content
Browse files

Merge pull request #4 from pagameba/paul

Updating init code callback logic
  • Loading branch information...
2 parents f0b198e + 235f116 commit 4e56a062c5ce271c8758a917d0c166af60574739 @scottburch scottburch committed
Showing with 24 additions and 14 deletions.
  1. +22 −13 couch-ar.js
  2. +2 −1 test/spec/TestUserSpec.js
View
35 couch-ar.js
@@ -1,3 +1,6 @@
+var checkLoaded = function() {};
+var domainLoadedCount = 0;
+
var cradle = require('cradle');
var fs = require('fs');
var Base = require('./Base');
@@ -39,13 +42,22 @@ exports.init = function(config, callback) {
});
function initDomainConstructors() {
- fs.readdirSync(config.root).forEach(function(filename) {
- /\.js$/.test(filename) && require(config.root + '/' + filename)
+
+ checkLoaded = function() {
+ if(!domainLoadedCount) {
+ callback(db);
+ }
+ }
+
+ var filenames = fs.readdirSync(config.root);
+ filenames.forEach(function(filename) {
+ /\.js$/.test(filename) && require(config.root + '/' + filename) && domainLoadedCount ++;
});
- callback(db);
+
}
}
+
/**
* Create a domain constructor. Use this in each domain file
*/
@@ -65,14 +77,13 @@ exports.create = function(name, config, constr) {
}
// Run all of the creators
- addCreateMethod(function() {
- addViews(function() {
- addFinders(function() {
- });
- });
+ addCreateMethod();
+ addViews(function() {
+ addFinders();
+ domainLoadedCount--;
+ checkLoaded();
});
-
factory.addView = function() {
addView.apply(factory, arguments);
}
@@ -81,7 +92,7 @@ exports.create = function(name, config, constr) {
exports[config.dbName] = exports[config.dbName] || {};
return exports[name] = exports[config.dbName][name] = factory;
- function addFinders(callback) {
+ function addFinders() {
for (prop in config.properties) {
addFinders(prop);
}
@@ -90,7 +101,6 @@ exports.create = function(name, config, constr) {
}
addFinders('id');
addList();
- callback();
function addFinders(finderName) {
@@ -206,7 +216,7 @@ exports.create = function(name, config, constr) {
/**
* Add the create() static method to the factory
*/
- function addCreateMethod(callback) {
+ function addCreateMethod() {
factory.create = function(props) {
var obj = factory();
for (var n in props) {
@@ -217,7 +227,6 @@ exports.create = function(name, config, constr) {
obj.rev = obj.rev || props._rev || props.rev;
return obj;
}
- callback && callback();
}
}
View
3 test/spec/TestUserSpec.js
@@ -29,7 +29,8 @@ describe('couch-ar', function () {
},
function () {
// delay so that everything can be setup
- setTimeout(asyncSpecDone, 500);
+// setTimeout(asyncSpecDone, 500);
+ asyncSpecDone();
}
);
asyncSpecWait();

0 comments on commit 4e56a06

Please sign in to comment.
Something went wrong with that request. Please try again.