【问题标题】:Uncaught TypeError: Cannot read property 'app' of undefined未捕获的类型错误:无法读取未定义的属性“应用程序”
【发布时间】:2013-07-23 13:05:09
【问题描述】:

我有一个复合视图:

var resultView = Marionette.CompositeView.extend({
        template : ResultPanel,
        itemView : ResultItemView,
        initialize : function() {
            ...
        },
        itemViewOptions : {
            app : this.options.app
        },

我只想将此视图的 app 属性分配给 itemView 的 app 属性。所以我可以从其他视图使用这个视图的应用程序。但我收到此错误:未捕获的类型错误:无法读取未定义的属性“应用程序”。我究竟做错了什么?有没有其他方法可以做到这一点?

【问题讨论】:

  • 你想通过构造函数传递app变量吗?以这种方式声明时,this 指的是 {} 对象

标签: javascript backbone.js view marionette


【解决方案1】:

可能性 #1: this.option 在您的代码执行时尚未设置。

可能性 #2: 也许“这”不是你所期望的。 分配 var that = this; 之前并使用“那个”而不是“这个”。

或分配 var _options = this.options;在extend() 之前并在extend 中使用_options。

【讨论】:

    【解决方案2】:

    options 将从您提供给视图构造函数的对象中生成。它包括除modelcollection 之外的所有内容

    var rv = new resultView({model: something, app: something})
    

    然后可以这样访问

    var resultView = Marionette.CompositeView.extend({
            itemView : ResultItemView,
            initialize : function(options) {
                 this.app = options.something;    
            },
            itemViewOptions : {
                app : this.options.app
            },
    

    如果您想在其他方法中引用这些选项,则需要将所需的变量附加到视图 (this) 本身。

    您无法从 options 参数中访问 model 之类的内容,但是它们会自动附加到您的视图中

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2015-01-06
      • 2017-07-26
      • 2018-11-09
      • 2019-02-26
      • 2015-07-11
      • 1970-01-01
      相关资源
      最近更新 更多