Skip to content

Commit

Permalink
Merge pull request #4 from pagameba/paul
Browse files Browse the repository at this point in the history
Updating init code callback logic
  • Loading branch information
scottburch committed Feb 1, 2012
2 parents f0b198e + 235f116 commit 4e56a06
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
35 changes: 22 additions & 13 deletions couch-ar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var checkLoaded = function() {};
var domainLoadedCount = 0;

var cradle = require('cradle');
var fs = require('fs');
var Base = require('./Base');
Expand Down Expand Up @@ -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
*/
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -90,7 +101,6 @@ exports.create = function(name, config, constr) {
}
addFinders('id');
addList();
callback();


function addFinders(finderName) {
Expand Down Expand Up @@ -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) {
Expand All @@ -217,7 +227,6 @@ exports.create = function(name, config, constr) {
obj.rev = obj.rev || props._rev || props.rev;
return obj;
}
callback && callback();
}

}
Expand Down
3 changes: 2 additions & 1 deletion test/spec/TestUserSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ describe('couch-ar', function () {
},
function () {
// delay so that everything can be setup
setTimeout(asyncSpecDone, 500);
// setTimeout(asyncSpecDone, 500);
asyncSpecDone();
}
);
asyncSpecWait();
Expand Down

0 comments on commit 4e56a06

Please sign in to comment.