question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow use of a read-only variable in static command bindings

See original GitHub issue

Some time ago I’ve found myself in a situation where I wanted to update multiple viewmodel properties in one button click. I wanted to load both user information and a shopping cart content to fill the top panel of my page when a button is clicked. Let’s have a viewmodel:

public class ThePage
{
    public ShopingCartViewModel ShoppingCart { get; set; }
    public UserInfo { get; set; }
    //...
}

Of course the viewmodel contains lots of other properties, and I don’t want to drag them all to the server side and back just because I need to update 2 properties… …so I used a service with contract like this:

public class PanelInfoDto{
    public ShopingCartViewModel ShoppingCart { get; set; }
    public UserInfo { get; set; }
}

public interface IPanelInfoService
{
    ShopingCartViewModel  GetShoppingCart();
    UserInfo  GetUserInfo();
    PanelInfoDto GetPanelInfo();
}

I realized, that there is no practical way to update two properties at once using single service call inside of static command.

Only 2 ways to solve this now:

  1. Using two calls: staticCommand: ShoppingCart = service.GetShoppingCart(); UserInfo = service.GetUserInfo(). This is less then ideal because I need to make 2 calls to the server which is slower. This method does not work for all cases. There are operations that require updating multiple properties which cannot be repeated (Save, Refresh of items,…).

  2. Using temporary viewmodel property: staticCommand: Temp = service.GetPanelInfo(); UserInfo = Temp.UserInfo(); ShoppingCart = Temp.ShoppingCart. This method also works for operations that cannot be repeated. However I find it clunky and hard on an unfamiliar reader.

My proposed solution:

<dot:Button Text="Update" 
            Click="{staticCommand: let info = service.GetPanelInfo(); 
                                              UserInfo = info.UserInfo(); 
                                              ShoppingCart = info.ShoppingCart}" />

Read-only binding local variable that would be defined in the first statement and could be used in all following statements in the same binding.

This variable would be also useful in following use cases:

  • saving data and getting updated repeater items back together with success/error message
  • updating 2 grids at once
  • updating grid and filling items of dependent combo-boxes at the same time
  • updating multiple filters that depend on each others values

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
tomashercegcommented, Oct 17, 2019

@martindybal This doesn’t support Property.Property = something, but instead of calling Context.UpdateProperties we can return something like ViewModelPatch<T>:

return new ViewModelPatch<T>(vm => vm.UserInfo = "aaa", vm => vm.ShoppingCart = cart);

And then call it like this:

{staticCommand: _page.patch(_this, _service.GetPanelInfo())}

We might also detect that we are adding or removing items from the collection and serialize the ViewModelPatch object so we can repeat the operation in JS.

0reactions
exyicommented, Jun 16, 2021

This was implemented in #869

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing a static readonly value as a CommandParameter ...
If I want to send "MyWidth" as a command parameter to a button, how do I set up the xaml for that? <Button...
Read more >
How to use const, readonly, and static in C# | InfoWorld
Use the readonly keyword when you are not sure whether the value of a variable of an object needs to change but you...
Read more >
Difference Between Const, ReadOnly and Static ...
A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's...
Read more >
Difference between static, readonly, and constant in C#
Therefore, readonly variables are used for the run-time constants. The constant fields must be initialized at the time of declaration.
Read more >
Properties - Manual
A readonly property can only be initialized once, and only from the scope where it has been declared. Any other assignment or modification...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found