Given:
StringPreconditions
extendsObjectPreconditions
ObjectPreconditions
depends onStringPreconditions
(one of its methods returns the subclass)Preconditions
is a gatekeeper toObjectPreconditions
andStringPreconditions
(making sure they both load before returning an instance)User
depends onPreconditions
I have this code:
define(["ObjectPreconditions"], function(ObjectPreconditions)
{
console.log("Inside StringPreconditions");
function StringPreconditions() {}
StringPreconditions.prototype = Object.create(ObjectPreconditions.prototype);
StringPreconditions.prototype.constructor = ObjectPreconditions;
return StringPreconditions;
});
define(["require"], function(require)
{
console.log("Inside ObjectPreconditions");
// Resolve circular dependencies
var StringPreconditions;
require(["StringPreconditions"], function(theStringPreconditions)
{
StringPreconditions = theStringPreconditions;
console.log("ObjectPreconditions finished loading StringPreconditions");
});
function ObjectPreconditions() {}
ObjectPreconditions.prototype.isInstanceOf(type)
{
console.log("ObjectPreconditions.isInstanceOf() invoked");
if (type === String)
return new StringPreconditions();
}
});
define(["ObjectPreconditions", "StringPreconditions"], function(ObjectPreconditions, StringPreconditions)
{
console.log("Inside Preconditions");
var Preconditions = {};
Preconditions.requireThat(parameter) = function()
{
return new ObjectPreconditions(parameter);
};
return Preconditions;
});
define(["Preconditions"], function(Preconditions)
{
console.log("Inside User");
function User() {}
User.prototype.doSomething = function()
{
var StringPrecondition = Preconditions.requireThat("test").isInstanceOf(String);
}
});
The problem is that 10% of the time I get this load order:
- Inside User
- Inside Preconditions
- Inside ObjectPreconditions
- Inside StringPreconditions
- ObjectPreconditions.isInstanceOf() (CRASH because StringPreconditions is undefined)
- ObjectPreconditions finished loading StringPreconditions
I've already read http://requirejs.org/docs/api.html#circular but I believe they are doing the same thing I am.
Any ideas?