Javascript BackboneJS视图-未捕获类型错误:无法读取属性';获取';未定义的
我有以下主干视图,其中包含一个渲染函数,如下所示:Javascript BackboneJS视图-未捕获类型错误:无法读取属性';获取';未定义的,javascript,backbone.js,marionette,Javascript,Backbone.js,Marionette,我有以下主干视图,其中包含一个渲染函数,如下所示: render: function () { this.emptyContent(); if (this.model.get('ChatsInQueue') == -1) { this.model.set('ChatsInQueue', '-'); } this.$el.append(this.template({ Stats: this.model.toJSON(), Prior: this.model.
render: function () {
this.emptyContent();
if (this.model.get('ChatsInQueue') == -1) {
this.model.set('ChatsInQueue', '-');
}
this.$el.append(this.template({ Stats: this.model.toJSON(), Prior: this.model.get('PriorDayStats') }));
var self = this;
this.graphView.model.fetch({
success: function () {
self.graphView.render(this.model.get("TotalCalls"), this.model.get("CallsAbandoned"));
}
});
this.tickerTapeText();
$(window).trigger('resize');
但是,当我运行应用程序时,我收到以下控制台错误:
Uncaught TypeError: Cannot read property 'get' of undefined
at success
有人能帮我诊断一下我的成功函数需要更改什么,以便定义“get”吗 JavaScript的
函数
绑定自己的this
,这与创建它的上下文的this
不同。在ES6中,作为一个不绑定自己的this
的函数的替代方案引入了MDN web文档(除其他外,请参阅MDN web文档)
您已经通过声明一个变量
self
并将this
赋值给它并将其用于self.graphView.render
调用来解决这个问题。但是,在参数中,您仍然使用this
而不是self
。在那里也可以使用self
,如果您的环境实现了箭头函数,则最好使用箭头函数 太明显了!谢谢我在以下代码中收到了相同的错误:u.each(this.model,function(client){this.createGraphData(client.get(“TotalCalls”),client.get(“CallsAbandoned”);};有什么想法吗?是的,情况也是一样(请参见this.createGraphData
)<代码>此在函数内部
将是不同的此
。使用self
-变量再次捕获外部this
,或使用箭头函数。我有这个,但它不起作用:var self=this;\ux。每个(self.model,function(client){self.createGraphData(client.get(totalCalls),client.get(callsAbandoned));您是否愿意编写出您潜在的解决方案?