Is there such a thing as a page instance variable?

I’ve noticed that the only way to create a variable is either through global variables or component variables. But is there a way to create a variable that is only for a specific page?

I’m asking because if I have to create a global variable every time I need a variable, then two things are going to happen:

  1. The names are going to have to be descriptive so that I and others understand where that variable is being used (i.e., pageNameVariableNameWhichCouldStatGettingLong and what happens if I change the pageName?!).
  2. There’s a potential to have an overwhelming list of global variables!
1 Like

The trick is to effectively plan out a folder structure that’s easy to understand for your variables. You can also bundle multiple variables into one by using an object variable and structuring your needs in that.

1 Like

Thanks @Micah

That is helpful and gives me some idea to organize things – particularly using an object. I guess I can create one object per page and use key-value pairs for each variable that’s needed for that page. Does that sound like a good idea?

I’m wondering if you can elaborate more on the folder structure. What would you propose?

The sad thing about folders is that they don’t show up in the list when picking a variable to use (see screenshot). So, it would seem, at least for the moment, that naming your variable with a scope (e.g., prefixing with the page name) would be a better way to prevent naming collisions and also make the variable easier to find.

CleanShot 2024-05-15 at 08.40.22

I am advocating for variable per page internally for a long time, I have added this topic to the product discussion :slight_smile:

5 Likes

Thanks @aurelie!

Just in case it goes that way, are you saying one Object per page that captures all the variables for that page? Or what do you mean?

Just an idea, curious what you think…

This is a workaround…

What if we create a component to represent a page, solely to store the page’s variables?

So one component per page… essentially some type of ViewModel?