You can get data for the Typeahead via Ajax in two ways:
- Fetch all data before activating the Typeahead
- Fetch data as user types into the input field
When to use Method 1
You want to use method 1 if you have a small amount of data to fetch. If you have less data, users need to wait a lesser amount of time before they use the Typeahead.
Also, you wonβt have to worry about debouncing, optimising performance, and all the complicated ajax stuff.
When to choose Method 2
You only want to use Method 2 if:
- You have a large amount of data (too much to download).
- You cannot predict what users will type.
A good example here is Google. You canβt predict what users would search for. Trying to download the entire search index is going to be way too expensive for users.
Other good examples include search results.
What weβll use
Since the Typeahead requires a small amount of data, weβll use the first method.
Welcome! Unfortunately, you donβt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson