0

I'm trying to use Jquery UI's autocomplete and have the following code

$( '#searchbar' ).autocomplete
source: ( request, response ) ->
$.ajax({
url: "$$ apiUrl pt.casemanagement $$/case/search"
})
success: ( data ) ->
            response([
              { label: "Example", value: 'Testing'}
            ])

I'm basically just waiting for the success and injecting a random label and value into the response for testing purposes. The bar is definitely sending the GET and getting a 200. I can even see the values from the server, but for whatever reason it doesnt seem like anything is being populated in the searchbar. I should be able (as far as I understand) to type 'E' and see 'Example' auto suggested, right?

Does this syntax seem wrong?

1
  • It seems that the success handler is not firing, although I'm checking the JSON returned by the server and it passes JSONLint. Commented Apr 21, 2014 at 20:45

1 Answer 1

1

Your syntax is indeed wrong. The main issue with it is indentation. Blocks are defined with indentation in CoffeeScript, so you probably wanted something like this:

$( '#searchbar' ).autocomplete
  source: ( request, response ) ->
    $.ajax({
      url: "$$ apiUrl pt.casemanagement $$/case/search"
      success: ( data ) ->
        response([
          { label: "Example", value: 'Testing'}
        ])
    })

Also, you were closing the options object to the $.ajax call before finishing the literal object definition. I didn't change any other whitespace or syntax to make it clear what the indentation implies.

In CoffeeScript, you can omit parens for function calls with arguments and braces around objects (unless it's ambiguous or you need to bypass the default precedence rules), so your code would look cleaner like this:

$('#searchbar').autocomplete
  source: (request, response) ->
    $.ajax
      url: "$$ apiUrl pt.casemanagement $$/case/search"
      success: (data) ->
        response [
          label: 'Example'
          value: 'Testing'
        ]

If you need further details about why and how this works, let me know and I can go line-by-line over it.


Sidenote: you're not using the request parameter to get the autocomplete values from the server. That means you probably won't get the expected results from the server, since you're not sending it the search term.

Sign up to request clarification or add additional context in comments.

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.