FnzBoostrapComponentBase.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using FNZCM.BlazorWasm.UI.Components.Bases;
  2. using Microsoft.JSInterop;
  3. using Newtonsoft.Json;
  4. namespace FNZCM.BlazorWasm.UI.Components.FnzBoostrap.Bases
  5. {
  6. public class FnzBoostrapComponentBase<T, TOptions> : FnzComponentBase, IAsyncDisposable
  7. where T : class
  8. where TOptions : OptionsBase, new()
  9. {
  10. protected DotNetObjectReference<FnzBoostrapComponentBase<T, TOptions>> dotNetRef { get; }
  11. public FnzBoostrapComponentBase() => dotNetRef = DotNetObjectReference.Create(this);
  12. public Guid InstanceId { get; } = Guid.NewGuid();
  13. public string ElementId => Prefix + "-" + InstanceId.ToString("n");
  14. protected virtual string Prefix => "fnz.boostrap";
  15. protected TOptions _options = new TOptions();
  16. protected override async Task OnAfterRenderAsync(bool firstRender)
  17. {
  18. if (firstRender)
  19. {
  20. await JSRuntime.InvokeVoidAsync($"{Prefix}.init", ElementId, JsonConvert.SerializeObject(_options), dotNetRef);
  21. _options.ResetChanged();
  22. }
  23. else
  24. {
  25. if (_options.CheckIsChanged())
  26. {
  27. await JSRuntime.InvokeVoidAsync($"{Prefix}.sync", ElementId, JsonConvert.SerializeObject(_options), dotNetRef);
  28. _options.ResetChanged();
  29. }
  30. }
  31. }
  32. public virtual async ValueTask DisposeAsync()
  33. {
  34. await JSRuntime.InvokeVoidAsync($"{Prefix}.dispose", ElementId, dotNetRef);
  35. dotNetRef.Dispose();
  36. }
  37. }
  38. }