title: Dojo 1.9: placeAt automatically calls startup when parent widget has started up
body: <p><a href="http://dojotoolkit.org/" rel="nofollow">Dojo v1.9</a></p>
  
  <p>I am writing some code that progrommatically creates a widget that I need to place into a layout widget. The problem I currently have is that <code>startup</code> is automatically called over aggressively by dojo.</p>
  
  <p>If the layout widget has already started up, then <code>startup</code> is called when <code>placeAt(domNode)</code> is used. This is not desirable, it would be better to have to explicitly call <code>startup</code>.</p>
  
  <p>I say this because with the current situation, <code>startup</code> is called twice, once for when <code>placeAt</code> is called and once for when <code>startup</code> is called manually straight after that by me.</p>
  
  <p>The <a href="https://dojotoolkit.org/reference-guide/1.9/dijit/_WidgetBase.html#dijit-widgetbase" rel="nofollow">Dojo docs</a> and <a href="https://dojotoolkit.org/documentation/tutorials/1.9/understanding_widgetbase/" rel="nofollow">related tutorials</a> say that when creating widgets programmatically you should always call <code>startup</code> manually to be sure correct parsing occurs. But the automatic calling of <code>startup</code> is causing some problems, with duplicate actions occuring on startup, I could move these into <code>postCreate</code>, but that is just hiding the problem.</p>
  
  <p>Here is a simple example of the widget creation/startup...</p>
  
  <pre><code>var myWidget = new Widget();
  myWidget.placeAt('mainContent');
  myWidget.startup();
  </code></pre>
  
  <p><code>mainContent</code> is a div within a ContentPane, here is the layout html...</p>
  
  <pre><code>&lt;body class="claro"&gt;
      &lt;div id="appLayout" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design: 'headline'"&gt;
          &lt;div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region: 'center'"&gt;
              &lt;div id="mainContent"&gt;&lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="edgePanel" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region: 'top'"&gt;Some text here&lt;/div&gt;
          &lt;div id="leftCol" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region: 'left'"&gt;&lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>The code works, its really the issue of <code>startup</code> being called twice that I'm trying to resolve.</p>
  
  <hr>
  
  <p>UPDATE: WORKAROUND</p>
  
  <p>Suggested checks inside the <code>startup</code> function against the widget private variable <code>_started</code> are at least protecting me from duplication, here is an example of my check.</p>
  
  <pre><code>startup: function(){
      if (typeof this._started === "undefined"){
          // call inherited and pass on arguments
          this.inherited(arguments);
  
          // then do what ever you need here
      }
  }
  </code></pre>
  
  <p>Notice I had to use <code>typeof</code> and not a <code>boolean</code> check, even though after <code>startup</code> the value  of <code>_started</code> is <code>true</code>. This was because I discovered the variable isn't actually declared until <code>startup</code> has run at least once, something to look out for.</p>
  
  <p>Anyway, this offers me a workaround, but doesn't fix the issue of <code>startup</code> being called more than once by dojo.</p>