lokijs

What is DynamicView in layman's term in LokiJS?


I kind of understand what DynamicView is. However, at the same time, I am confused by what it really means.

I know how to use it based on articles I have read such as here. However, what is it?


Solution

  • Imagine you have a db, you run a query like "give me all the users that are marked as logged in". You obtain that result back and you process it for the purposes of your application. If you run that same query at a different time, you get a different result set. In LokiJS, Collections are event emitters, and Dynamic Views are registered to listen to those events. Which means they are aware of the changes that occurred in the Collection, and they apply it to the result set of the view. In the above case, if you create a Dynamic View, whenever you call data() you get the result corresponding to the current status of the database, not when the view was created. You run a query once, and you can forget about updating its results, the Dynamic View does that for you (through the data() method, which is lazy). This can be very helpful - but it also prevents you from storing a history of states in your application, so use with caution! The correct definition for a dynamic view is self-materializing view.