Javascript Uncaught TypeError : Object has no method
in my javasc开发者_如何学Cript application I am getting the following error upon page load.
Uncaught TypeError : Object #<Object> has no method 'showHideCleanupButton'
this appears (inside Chrome's debugger) to be blowing up inside the method 'getAllItemsForDisplay'where it calls 'this.showHideCleanupButton'
here is a link to it on jsFiddle: http://jsfiddle.net/cpeele00/4fVys/
Any help would be greatly appreciated :-)
Thanks,
Chris
It appears that this is the relevant piece of code:
getAllItemsForDisplay: function() {
$.getJSON('services/act_getAllItems/', function(data) {
$('#items-list').empty();
$('#items-listTmpl').tmpl(data).appendTo('#items-list');
this.showHideCleanupButton();
BeefyUtils.noSelect();
});
},
What are you expecting this
to be set to inside the getJSON
callback? Have you set a breakpoint on the relevant statement and looked at what the value of this
is?
If I understand the jQuery doc correct, by default this
inside a getJSON call will be a reference to the ajax options that were originally passed in. I think it's possible to change what this
will be with ajaxSetup, but I don't see that you've done that.
If you want to refer to the this
at the beginning of getAllItemsForDisplay
, then you need to save that into another variable that you can use like this:
getAllItemsForDisplay: function() {
var obj = this;
$.getJSON('services/act_getAllItems/', function(data) {
$('#items-list').empty();
$('#items-listTmpl').tmpl(data).appendTo('#items-list');
obj.showHideCleanupButton();
BeefyUtils.noSelect();
});
},
To piggy-back on jfriend00's answer, change this:
getAllItemsForDisplay: function() {
$.getJSON('services/act_getAllItems/', function(data) {
$('#items-list').empty();
$('#items-listTmpl').tmpl(data).appendTo('#items-list');
this.showHideCleanupButton();
BeefyUtils.noSelect();
});
},
to this:
getAllItemsForDisplay: function() {
var self = this;
$.getJSON('services/act_getAllItems/', function(data) {
$('#items-list').empty();
$('#items-listTmpl').tmpl(data).appendTo('#items-list');
self.showHideCleanupButton();
BeefyUtils.noSelect();
});
},
This will give you the context that called getAllItemsForDisplay, which I assume is what you want.
精彩评论