Search code examples
ember.jsember-data

How can I reload a model on controller checkbox state change?


Given some checkboxes that mut a boolean, how can we automatically update the model using these booleans by querying the datastore in the route?

E.g.:

model(params) {
  return this.store.query('email', {
    filter: {
      option1: controller.checkbox1, 
      option2: controller.checkbox2, 
      option3: controller.checkbox3
    }
  })
}

Solution

  • There are two established patterns:

    1. Using query parameters for storing the filter values used in API query. Query params support reloading the model hook on changes out of the box.
    2. A DS.AdapterPopulatedRecordArray, which store.query() resolves with, provides an update() method that will reload the query.

    I would recommend query parameters in most cases as that also allows to share and bookmark the application state including the filters. It does not add a lot complexity if it's just about boolean values.

    You could also the RouterService to transition to the same route which works similar to refreshing the route. But it has some drawbacks as it adds more complexity compared to the other options and the default query parameters will be included in the route. See method documentation and the RouterService RFC for more details on the last one.