title: Resolving Circular Dependency in NodeJS model
body: <p>I have two models (using BookshelfJS, or BackboneJS, doesn't matter): <code>modelA</code>, <code>modelB</code>. <code>modelA</code> requires <code>modelB</code> and <code>modelB</code> requires <code>modelA</code>. </p>
  
  <p>Initially, I had:</p>
  
  <pre><code>// modelA
  var Bookshelf = require('bookshelf);
  var modelB = require('./modelB');
  var modelA = Bookshelf.Model.extend({});
  
  module.exports = modelA;
  
  // modelB
  var Bookshelf = require('bookshelf);
  var modelA = require('./modelA);
  var modelB = Bookshelf.Model.extend({});
  
  module.exports = modelB;
  </code></pre>
  
  <p>Then I read <a href="https://github.com/tgriesser/bookshelf/wiki/Plugin:-Model-Registry" rel="nofollow">here</a> about the <code>Bookshelf.plugin('register')</code>. So I changed my code to:</p>
  
  <pre><code>// modelA
  var Bookshelf = require('bookshelf);
  var modelB = Bookshelf.model('modelB');
  var modelA = Bookshelf.Model.extend({});
  
  Bookshelf.model('modelA', modelA);
  
  // modelB
  var Bookshelf = require('bookshelf);
  var modelA = Bookshelf.model('modelA');
  var modelB = Bookshelf.Model.extend({});
  
  Bookshelf.model('modelB', modelB);
  </code></pre>
  
  <p>In case 1, there was a circular dependency, which was fixed in case 2. But now in case 2, when <code>modelA</code> is being bootstrapped, <code>modelB</code> is not defined yet, so <code>Bookshelf.model('modelB')</code> doesn't exist. </p>
  
  <p>What do I do?</p>