Persist view variables for manual query filters

I have some filters that I allow a User to define for a query using view variables. These are a combination of DB objects and normal data variables. I want the selected filters to be persisted when I navigate away from the view and back to it.

How do I do this?

Broadly speaking I would say there are two main options to persist the selections.

  1. Store the selections in the DB
  2. Store the selections in the LocalDB


For "1. Store the selections in the DB" you again have 2 broad options

  1. Save all the selections to the user object.

For this to work you will need dedicated fields and relationships on the user model to store the selections. When the selections are changed by the user you simply update the corresponding fields and relationships on the user model. You can even go so far as to make your view selections bind directly to the user object and let your query get populated from values stored in those fields and relationships on the user object as opposed to the view variables.

  1. Save all the selections to a dedicated DB object that can be shared by other users.

For this to work you will need a dedicated DB model with all the necessary fields and relationships as well as an object of that model to store the selections, you can have one object per user, one per region, one global one or any other configuration that works for your use case. When the selections are changed by a user you simply update the corresponding fields and relationships on the relevant object. You can continue to work with dedicated view variables or you can bind the view components directly to the relevant object for that user. Your query can once again get populated either from the view variables, if you are still using them, or from the dedicated object that is storing the selections.


For "2. Store the selections in the LocalDB" there is essentially just one option.

Since data in the LocalDB is not shared between devices this cannot be used to share the filter selections between users or devices, even if it's the same user with multiple devices.

Here again you will need a dedicated model to store the user's selections. As the user changes the selections you need to update the relevant object in the LocalDB with the new values. The big difference here is with relationships. You cannot reference objects that exist the DB from objects that exist in the LocalDB using normal relationship syntax. What you need to do instead is to store the ID of the DB object in a field on the LocalDB object. Then when you want to retrieve the DB object you find it using the ID that you stored in the LocalDB. Something like this.

function saveToLocalDB() {
   var filter = LocalDB.filter.first();
   filter.some_value = view.some_value;
   filter.some_obj_id = view.some_obj.id;
   filter.save();
}

function retrieveFromLocalDB() {
   var filter = LocalDB.filter.first();
   view.some_value = filter.some_value;
   view.some_obj = DB.obj_type.first(filter.some_obj_id);
}

The first function saves the selections to the object in the LocalDB, the second function retrieves the selections from the values that got stored in the LocalDB.

I hope this makes sense - but for clarification below is what you cannot do in terms of relationship syntax between DB and LocalDB

function saveToLocalDB() {
   var filter = LocalDB.filter.first();
   filter.some_value = view.some_value;
   filter.some_obj(view.some_obj) // trying to set a relationship in the LocalDB using a normal DB object. view.some_obj is a DB object, filter is a LocalDB object, you cannot have a relationship between them;
   filter.save();
}

function retrieveFromLocalDB() {
   var filter = LocalDB.filter.first();
   view.some_value = filter.some_value;
   view.some_obj = filter.some_obj(); // THIS WILL FAIl: filter.some_obj() will be null
}

1 Like