admin管理员组文章数量:1391947
var resultItemView = Marionette.CompositeView.extend({
render : function(){
google.load("visualization", "1", {packages:["table"], callback: function() {
var self = this;
this._drawVisualization(self);
}});
},
_drawVisualization : function(self){
var data = new google.visualization.DataTable();
//Here i'm creating data table ...
var chart = new google.visualization.LineChart(self.$el.find("#graphDiv"));
chart.draw(data, null, null);
},
return resultItemView;
});
At render function google visualization is being loaded and in callback function drawVisualization is being called. Also i'm passing "this" object to this function as a parameter.To do this i used an anonymous function. In drawVisualization function self is equal to this "this" object. But i'm getting this error: "Uncaught TypeError: Object [object global] has no method '_drawVisualization'". What am i doing wrong? How can i correct it? Thanks for help.
var resultItemView = Marionette.CompositeView.extend({
render : function(){
google.load("visualization", "1", {packages:["table"], callback: function() {
var self = this;
this._drawVisualization(self);
}});
},
_drawVisualization : function(self){
var data = new google.visualization.DataTable();
//Here i'm creating data table ...
var chart = new google.visualization.LineChart(self.$el.find("#graphDiv"));
chart.draw(data, null, null);
},
return resultItemView;
});
At render function google visualization is being loaded and in callback function drawVisualization is being called. Also i'm passing "this" object to this function as a parameter.To do this i used an anonymous function. In drawVisualization function self is equal to this "this" object. But i'm getting this error: "Uncaught TypeError: Object [object global] has no method '_drawVisualization'". What am i doing wrong? How can i correct it? Thanks for help.
Share Improve this question asked Jul 29, 2013 at 10:26 OzgOzg 4012 gold badges8 silver badges20 bronze badges1 Answer
Reset to default 6You need to put var self = this;
outside of the call to google.load
.
var resultItemView = Marionette.CompositeView.extend({
render : function(){
var self = this;
google.load("visualization", "1", {packages:["table"], callback: function() {
self._drawVisualization(self);
}});
},
This is because the anonymous function you pass as a callback parameter to google.load
will be called without specifying a this
context - so that will be the global (window) object.
Inside of your render
function, you will have the correct this
and can store that in a variable self
to refer to it in your callback function.
EDIT:
Also, if you do this, you do not need to pass the this
context to the _drawVisualization
method anymore. Complete Example:
var resultItemView = Marionette.CompositeView.extend({
render : function(){
var self = this;
google.load("visualization", "1", {packages:["table"], callback: function() {
self._drawVisualization();
}});
},
_drawVisualization : function(){
var data = new google.visualization.DataTable();
//Here i'm creating data table ...
var chart = new google.visualization.LineChart(this.$el.find("#graphDiv"));
chart.draw(data, null, null);
},
return resultItemView;
});
本文标签: javascriptUncaught TypeError Object object global has no methodStack Overflow
版权声明:本文标题:javascript - Uncaught TypeError: Object [object global] has no method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744702380a2620633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论