Passing Class Instances Between View Methods

Hi devs,

I’m running into some struggles with developing my energy system app.

I created classes that represent “energy assets”. So, in order to create and analyze an energy system, I create a class instance (asset) inside a view method. This works great for page 1 and allows me to visualize the energy asset.

But now when I reach page 2, I want to alter or add instance variables of the class instance (asset) and visualize this on the new page using a new view function. Is there a way to pass the class instance from the first view method to the second view method?

I tried a couple of things already:

  1. Created a function which creates the instance, this way I only have to call the function to get the instance. But this creates a new instance every time which is not preferred with future use of the app in mind.
  2. Defined an instance depository dict in the controller init, and tried to store my instances created in the view method in there. But this either gets cleared or not updated when reloading the second view.
  3. Created an InstanceDepository(dict) class to store the instances. But this way I need to pass the class as an argument in the view method call, which i cant find or is probably in the parametrization class Page(‘xxxx’, VIEWS=…), but I can’t pass any arguments here.
  4. The same problem (3.) occurs when using a depository dict as a Controller class “class-variable”.

Would you maybe have other suggestions?

Kind regards, Nino

Hi Nino,

Thank you for your question. As the VIKTOR platform is stateless, the results of any calculation done, whether that is for a view, or clicking a button, will need to be recalculated for any new calculation. There are steps one can take, however, to overcome this challenge. These are explained in detail via this link:

For your case I would recommend using memoize, especially if you are developing an editor-only app.

Let me if know if this solves your problem.