0

I'm trying to debug this code for a parentView. The error thrown from this code is "Uncaught TypeError: Cannot read property 'attributes' of undefined". The console.log also comes back as undefined, however if I console.log the same this.model.total_fans later in the render function, it has a value. I don't know backbone well enough to debug this annoyingly and potentially miniscule error, but I feel like something the way this is setup is wrong, especially after reading posts about dealing with rendering subviews within a masterview here How to handle initializing and rendering subviews in Backbone.js?. I'm wondering what all is wrong here. Should I instantiate the subviews within the parents initialize? And should the parent be calling render and pagesetup on model changes??

initialize:function () {
    var self = this;

    // General model for this page
    this.model = new geodemoModel();

    // separate summary model
    this.summaryModel = new summaryModel();

    this.model.fetch();
    this.summaryModel.fetch();

    this.model.bind('change', this.render, this);
    this.model.bind('change', this.pageSetup, this);

    this.summaryModel.bind('change', this.render, this);
    this.summaryModel.bind('change', this.pageSetup, this);

  },

pageSetup:function () {
 if (this.oneSelect == 'All' || this.oneSelect == 'lifetime_fans') {
        console.log(this.model.total_fans);
        var viewLifetime = new lifetimeView(this.model.total_fans.attributes.total_fans_object);
        $('#lifetimeFans').html(viewLifetime.render().el);
      }
Community
  • 1
  • 1
natecraft1
  • 2,737
  • 9
  • 36
  • 56

1 Answers1

1

Assuming your model exists, you should be using:

this.model.get('total_fans').attributes.total_fans_object

if that doesn't work, please paste a console.log(this.model) within pageSetup.

Prisoner
  • 27,391
  • 11
  • 73
  • 102
  • child {cid: "c7", attributes: Object, _changing: true, _previousAttributes: Object, changed: Object…} _changing: true _events: Object _pending: false _previousAttributes: Object attributes: Object dates: Object previous: Object selected: Object __proto__: Object changed: Object cid: "c7" queryParams: Object top_engagement_by_country_28: child top_engagement_demographics_28: child top_fans_by_country_28: child top_fans_demographics_28: child top_reach_by_country_28: child top_reach_demographics_28: child total_fans: child total_fans_demo: child – natecraft1 Nov 18 '13 at 18:05