Trunk: collection is not passed to view with Require.js

I am rewriting a Backbone.js app trying to use AMD's approach. I downloaded AMD-ified versions of Backbone and Underscore. I have checked and the jQuery, Backbone and Underscore call is being called. This is a pretty basic project, but for some reason my collection is no longer being passed to my view. I am new to AMD.

Here is my model:

define([
    'underscore',
    'backbone'
], function(_, Backbone) {
    var tableModel = Backbone.Model.extend({});
    return tableModel;
});

      

Here is my collection:

define([
    'jquery',
    'underscore',
    'backbone',
    'models/tableModel'
],
function($, _, Backbone, tableModel) {
    var tablesCollection = Backbone.Collection.extend({
        url: this.url,  // passed into collection at runtime, so same code can process multiple sets of data
        model: tableModel,
        initialize: function(models, options) {
            if (options && options.url) {
                this.url = options.url;
            }
            this.fetch({
                success: function(data, options) {
                    if ($.isEmptyObject(data.models)) {
                        App.Collections.Tables.NoData(data);
                    }

                }
            });
        }
    });

    return tablesCollection;
});

      

Here is my view:

define([
    'jquery',
    'underscore',
    'backbone',
    'models/tableModel',
    'collections/tablesCollection',
    'views/tableView'
],
function($, _, Backbone, tableModel, tablesCollection, tableView) {
    var tv = Backbone.View.extend({
        tagName: 'div',
        initialize: function() {
            console.log(this.collection);  // returns collection and undefined 
            this.collection.on('reset', this.render, this);  // errors: this.collection is undefined
        },
        render: function() {
            return this;
        }
    });

    return tv;
});

      

This is where the view and collection are created:

define([
    'jquery',
    'underscore',
    'backbone',
    'models/tableModel',
    'collections/tablesCollection',
    'views/tablesView'
], function($, _, Backbone, tableModel, tablesCollection, tablesView) {
    var t = new tablesCollection(null, { url: 'main-contact'} );
    var tables = new tablesView({ collection: t, template: 'main-contact-template'});
    $('#web-leads').html(tables.render().el);

});

      

Why am I getting function (){return c.apply(this,arguments)}

returned when I am console.log(tablesCollection)

? It looks like the collection is failing. Could this be a problem? My project is structured with a folder js

with subfolders called collections

, models

and views

. If I do console.log(this)

, I get: enter image description here

My data is there, but is this what I need? Why am I not getting my collection when I try it console.log

?

+3


source to share


2 answers


Don't use tablesCollection

directly. Use this.collection

. You have already initialized the view with it



0


source


What you see is the tablesCollection function, that is, the Backbone.Collection.extend (...) function.



The tablesCollection function is probably passed in as. However, you need to instantiate an object eg. new tablesCollection (null, {url: 'url'}) before entering the console, otherwise the shell extension function you see is displayed.

0


source







All Articles