sergserg
sergserg

Reputation: 22264

Using an anonymous function as part of a javascript object

I'm going to show you two snippets.

This works fine:

this.searchBox = new Foo.UI.SearchBox(this.input, {
    autoCompleteSearchComplete: processSearchResults
});

This doesn't work at all:

this.searchBox = new Foo.UI.SearchBox(this.input, {
    autoCompleteSearchComplete: function() {
        processSearchResults
    }
});

I need to place that processSearchResults call inside an if statement, to check if my search text input ($('.search')) has any text written inside it.

My first idea was to use this function type notation, but it's not working. It's as if the call to processSearchResults is never made at all.

Any suggestions?

Upvotes: 0

Views: 46

Answers (1)

Jon
Jon

Reputation: 437376

That's because you do not actually call that function. This would be correct:

this.searchBox = new Foo.UI.SearchBox(this.input, {
    autoCompleteSearchComplete: function() {
        if (...) {
            processSearchResults();
        }
    }
});

Upvotes: 4

Related Questions