Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You prior version of the code made more sense than this edited version, as in the prior version you were actually pushing to <code>console.log</code> the object that was giving trouble</p> <pre><code>// contents of 'that/view/that/gives/me/problems.js' define([ 'jquery', 'underscore', 'backbone', 'models/tableModel', 'collections/tablesCollection', 'views/tablesView', 'views/tableView' ], function($, _, Backbone, tableModel, tablesCollection, tableView) { console.log("factory", tablesCollection.collection); // &lt;- you did not have this line. my addition var tv = Backbone.View.extend({ tagName: 'div', initialize: function() { console.log("init", tablesCollection.collection); // &lt;- your prior version. Point of your error this.collection.on('reset', this.render, this); this.template = this.options.template; this.url = this.collection.url; } // ... }); return tv; }); </code></pre> <p>Now the reason you have it show up twice is because the view you define above <strong>is initialized twice somewhere</strong>.</p> <p>First time it's initialized it shows the value as you can see. The second time around the pointer <code>tablesCollection</code> is cleaned up by something and, hence, you have that error.</p> <p>Now what cleans up <code>tablesCollection</code> and why I don't see anywhere in the code you present. It has likely something to do with re-requiring 'collections/tablesCollection' module somewhere in your code. As I can see from your 'collections/tablesCollection' module code, you REDEFINE the output on every factory call. What I would do is calc it once and serve cached:</p> <pre><code>// contents of 'collections/tablesCollection.js' ;(function(){ var mythings = {} function initializer($, _, Backbone, tableModel){ return Backbone.Collection.extend({ url: 'url', model: tableModel, initialize: function(models, options) { // ... } }); } define([ 'jquery', 'underscore', 'backbone', 'models/tableModel' ], function($, _, Backbone, tableModel) { if (!mythings.tablesCollection){ // this will be done on first run. mythings.tablesCollection = initializer($, _, Backbone, tableModel) } // all others will just return same exact instance of collection class return mythings.tablesCollection }) })(); </code></pre> <p>EDIT:</p> <p><a href="https://groups.google.com/forum/?fromgroups=#!topic/amd-implement/IPpUuSV3OII" rel="nofollow">Asked the AMD spec group if there is a chance of 'factory' function rerunning</a> on every require. Immediate answer was "Not likely" but long term answer is "Possible (if asked under different name)" </p> <p>I added comment lines to the code snippet above indicating what files they are.</p> <p>I also added a console.log line in the first snippet that should help you understand that the issue you have is NOT with AMD loader. You simply <strong>initialize</strong> the view twice somewhere.</p> <p>When you run the code with comment lines like in the top snippet you will see <code>factory</code> log line shoing up only once and <code>init</code> line twice, like in your origianl error screenshot.</p> <p>You need to trace where you use the view returned in that <code>tv</code> variable. You are initing it twice.</p> <p>As to what happens to (what clears) reference to <code>tablesCollection</code> in the second run I don't know and don't see anywhere in the snippets you provided.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload