Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone console.log returning collection and undefined
    primarykey
    data
    text
    <p>Something odd is going on with my Backbone project. I'm rebuilding it as AMD and I'm having to change some variable names to get it working again. I have a collection I'm passing into a view, but when I <code>console.log</code> the collection, I get both the object and null. </p> <p>Here is my view: </p> <pre><code>define([ 'jquery', 'underscore', 'backbone', 'models/tableModel', 'collections/tablesCollection', 'views/tablesView', 'views/tableView' ], function($, _, Backbone, tableModel, tablesCollection, tableView) { var tv = Backbone.View.extend({ tagName: 'div', initialize: function() { console.log(this.collection); this.collection.on('reset', this.render, this); this.template = this.options.template; this.url = this.collection.url; }, render: function() { //tablesCollection.collection.each(this.addOne, this); return this; }, addOne: function(model) { var t = new tableView({ model: model, template: this.template, url: this.url }); this.$el.append(t.render().el); return this; }, stripQueryString: function(url) { return url.split('?')[0]; } }); return tv; }); </code></pre> <p>You'll see the console.log several lines down in the project. Here is what I get in Firebug as a result:</p> <p><img src="https://i.stack.imgur.com/8se7D.png" alt="enter image description here"></p> <p>Both cite the same line number. Here is what's in the object:</p> <p><img src="https://i.stack.imgur.com/sTyvP.png" alt="enter image description here"></p> <p>What is going on here? Why am I getting two results for the same thing? One of them is what I want and the other one isn't.</p> <p>EDIT: Here is where I instantiate the view:</p> <pre><code>define([ 'jquery', 'underscore', 'backbone', 'models/tableModel', 'collections/TablesCollection', 'views/tablesView', 'views/tableView' ], function($, _, Backbone, TableModel, tablesCollection, tablesView, tableView) { var t = new tablesCollection(null, { url: 'main-contact'} ); var tables = new tablesView({ collection: t, template: 'main-contact-template'}); $('#web-leads').html(tables.render().el); }); </code></pre> <p>Here is my collection:</p> <pre><code>define([ 'jquery', 'underscore', 'backbone', 'models/tableModel' ], function($, _, Backbone, tableModel) { var tablesCollection = Backbone.Collection.extend({ url: this.url, model: tableModel, initialize: function(models, options) { if (options &amp;&amp; options.url) { this.url = options.url; } this.fetch({ success: function(data, options) { } }); } }); return tablesCollection; }); </code></pre> <p>Two other files:</p> <pre><code>// Filename: app.js define([ 'jquery', 'underscore', 'backbone', 'router' // Request router.js ], function($, _, Backbone, Router){ var initialize = function(){ // Pass in our Router module and call it's initialize function Router.initialize(); }; return { //initialize: initialize &lt;--This is where the second init call was happening. }; }); </code></pre> <p>Main.js:</p> <pre><code> require.config({ paths: { //jquery: 'libs/jquery/jquery-1.8.3.min', underscore: 'libs/underscore/underscore-min', backbone: 'libs/backbone/backbone-min' } }); if ( typeof define === "function" &amp;&amp; define.amd &amp;&amp; define.amd.jQuery ) { define( 'jquery', [], function () { return jQuery; } ); } //the "main" function to bootstrap your code require(['jquery', 'underscore', 'backbone', 'app'], function () { var App = require('app'); App.initialize(); // console.log($); // console.log(_); // console.log(Backbone); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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