Uncaught TypeError: Cannot read property 'app' of undefined

荒凉一梦 提交于 2020-01-03 01:47:10

问题


I have a composite view:

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

I just want to assign this view's app property to itemView's app property. So i can use this view's app from other view. But I'm getting this error: Uncaught TypeError: Cannot read property 'app' of undefined. What am i doing wrong? Is there another way in order to do this?


回答1:


possiblity #1: this.option is not yet set when your code executes.

possibility #2: maybe 'this' is not what you expect it to be. assign var that = this; before and use 'that' instead of 'this'.

or assign var _options = this.options; before extend() and use _options in the extend.




回答2:


options will be generated from the object that you supply to the constructor of your view. It includes everything except for things like model and collection

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

Which can then be accessed like so

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

If you want to refer to these options in another method, you will need to attach the required variables to the view (this) itself.

You can't access things like the model from the options parameter, however they will automatically be attached to your view



来源:https://stackoverflow.com/questions/17827510/uncaught-typeerror-cannot-read-property-app-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!