0

in my javascript 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

2 Answers 2

1

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();
        });
    },
Sign up to request clarification or add additional context in comments.

4 Comments

In this context, this will be the context of the jQuery function that called the callback (or possibly the xhr object).
@tjameson - the doc for ajaxSetup seems to indicate that this will be set to point to the options object unless set differently in the options object. I don't have a testable example to look at it myself, but that's what I can glean from the jQuery doc.
In either case, it's probably not what the OP was expecting.
@jfriend00, thanks for the answer, it worked! thanks to everyone else who responded as well!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.