Search code examples
backbone.js

Search form model(s)


I have to design a search form and display the results. I am having some difficulties finalizing the models/collections to be used.

I have a few fields which acts as filters. These fields are select boxes where multiple items can be selected. After an item has been selected from a field, I need to call the API which will return a list of results with this filter and also all the remaining filter options for the other fields based on the first filter.

What I am confused is how should I go about this. Should every select box be a model? Because the options for each select box will be changing.

About the results, I am thinking that results should be a collection. But given that there is only one API endpoint here, I am confused which model should hold the url.


Solution

  • Since your API is actually returning a collection of results, I'd keep the filter parameters in a model, and while submitting search request, create the collection URL dynamically based on the filter model attributes.

    You can go the other way, send the request from model itself, then in parse set results from response to the collection and remove it from model, but it seems more hacky than dynamically creating collection URL