@druc if I were you I'll pass the 300 items.
Passing data to view vs fetching with ajax
Hi all,
I'm building a calorie tracker and it involves two lists of food items: one list with foods added by the user, one containing the default food directory (added by me).
I'm having a dilemma whether to pass data directly to view or fetching it using xhr requests.
Given the fact that I can control the default food directory size (let's say 300 items) should I pass the data directly to the view and use JS to allow the user to search the list, or I should go with xhr requests to filter the results?
The downside of using xhr is quite obvious - It's not as snappy as having the data already loaded. But what about passing the data directly? What are the downsides?
Thanks in advance
It really depends on the size of your default food list. Here are my opinions on both strategies:
- use Ajax requests
Ajax requests are incredibly fast and search functionalities with large data usually are populated with ajax requests. If you have a gigantic list of food I would definitely use Ajax and load only what the user searches for. You can easily implement that with tools like typeahead.
- pass data to view
If you will only have about 300 food items in that list, passing them to the view is not a bad idea as the loading time would not significantly affect your app's performance. It is also easy to sort and filter elements with JS. A tool like list.js would be useful in this case.
Please or to participate in this conversation.