Global Properties in Templates?

Most of the pages in my application are Store specific. I'd like to retrieve the last "store" during login from the user record during login and cache that in a global property. The Store selector is a dropdown that lives in the template so it's consistent across pages. Next step is to make each page use the selected store when retrieving/displaying data.

Are these global properties available when the template loads? I'm just starting to poke around and I'm getting errors:
"error CS1061: 'GlobalService" does not contain a definition for 'SelectedStore' and no accessible extension method 'SelectedStore' accepting a first argument of type 'GlobalService' could be found..."

Hi @ToddB,

You want to set a property in the MainLayout and access it from all application pages? We will add support for that in the next update before the end of this week.

2 Likes

Thanks for adding this! I'm having trouble using it though... as soon as I check the Global box, the project fails to build with "error cs0103: the name 'Globals' does not exist in the current context"

Am I doing something wrong?

Here is what I've tried and it worked for me:

DropDown in the main layout with change event handler to set global property orderId

Label in Orders page bound to global orderId property:



the result:

OK - Got this working... in that exact example, if you wanted to refresh the Orders datagrid or reload the whole app, what would be the best way to trigger that?

I did a test page with a label, and as soon as I change the dropdown, it updates the label as expected... but now I'm using the dropdown to set a Global Variable, then using that Global Variable value on the Stored Procedure parameter for the datagrid. Since the dropdown is in the template, I basically need the app to reload everything when the dropdown changes, as menu options could change depending on what's selected.

Try to execute StateHasChanged().

I actually did try that - didn’t work. Tried this.Load() as well.

Hi @ToddB,

You are right! At the moment it's not easy to reload a DataGrid filtered by a global property value. Calling StateHasChanged is not enough (we execute this automatically on global property change) and that's why we will re-execute page Load as well. Everything will be automatic - you don't have to add any additional code:

Hi @ToddB,

I spoke to soon. It will be better if we introduce new Change page event where you will able to execute Load() for the page. Not all devs will like auto reload of everything in case of global property change.

Awesome - glad you guys are on it!

It looked like from the What's New that this was solved - how do I use it? How do I trigger that Load()?

Thanks all!

Here is a simple example:

and here is with condition:

Thank you for the response. That sounds quite easy - but I tested without the condition and nothing is happening. From the output, it looks like it's reloading my dropdown box - but that's it.

Just to make sure we're clear - I have a dropdown box inside my main template - that is essentially a Store selector because everything on the page is dependent on which store I have selected. So even within the template, it should be reloading the page data just by using the await Load() ?

Thanks again,
-Todd

If the DropDown in your MainLayout sets a global property all pages will receive Change event. The screenshot that I’ve posted is from a page (not layout) and it will execute the Page Load event handler.

I’m afraid I don’t understand. You use the Change event of the Page to set a global property that will execute Load of the page and will trigger Change again. In my opinion this will cause endless loop.

Hey wasn't expecting a response during these hours - I was still doing some testing. I'm trying to figure out the best way to run this - I have a dropdown in the main layout - when it changes, it sets a global property. Then I need the content on that page to reload so the datagrids on whatever page I'm on reload with the new location set. Am I doing this all wrong?

The screenshot above is on the dropdown change event - I just looked closer at your example and saw that they're on the page change so I was trying to test and figure out how that works.

If this the DropDown Change event then the property set is ok however you will need to go to the page which you want to reload, add event handler to Page Change event and execute await Load().

OK That makes sense. I just tried it and ended up in an endless loop reloading the page - I'm trying to see if I have an extra event somewhere I shouldn't.

Got it working! Using the Condition got the job done. Thanks for your help and patience!

2 Likes