【问题标题】:backbone.js uncaught typeError in parentView trying to instantiate a subView试图实例化子视图的parentView中的backbone.js uncaught typeError
【发布时间】:2013-12-01 23:41:06
【问题描述】:

我正在尝试为 parentView 调试此代码。此代码引发的错误是“未捕获的类型错误:无法读取未定义的属性‘属性’”。 console.log 也以未定义的形式返回,但是如果我稍后在渲染函数中 console.log 相同的 this.model.total_fans ,它就有一个值。我不太了解主干,无法调试这个烦人且可能很小的错误,但我觉得这种设置方式是错误的,尤其是在阅读了关于在主视图中处理渲染子视图的帖子How to handle initializing and rendering subviews in Backbone.js? 之后。我想知道这里有什么问题。我应该在父母初始化中实例化子视图吗?父母是否应该在模型更改时调用渲染和页面设置?

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);
      }

【问题讨论】:

    标签: javascript backbone.js view render


    【解决方案1】:

    假设您的模型存在,您应该使用:

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

    如果这不起作用,请在pageSetup 中粘贴console.log(this.model)

    【讨论】:

    • child {cid: "c7", attributes: Object, _sharing: true, _previousAttributes: Object, changed: Object...}对象先前:对象选择:对象 proto:对象已更改:对象 cid:“c7” queryParams:对象 top_engagement_by_country_28:孩子 top_engagement_demographics_28:孩子 top_fans_by_country_28:孩子 top_fans_demographics_28:孩子 top_reach_by_country_28:孩子 top_reach_demographics_28:孩子 total_fans:孩子 total_fans_demo : 孩子
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2015-10-11
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 2013-10-21
    相关资源
    最近更新 更多