Browse Source

Persist component state for WebApp Scripts

pull/19410/head
Enis Necipoglu 3 years ago
parent
commit
f02af6326d
  1. 27
      framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpScripts.razor

27
framework/src/Volo.Abp.AspNetCore.Components.Web.Theming/Bundling/AbpScripts.razor

@ -1,5 +1,7 @@
@using Volo.Abp
@implements IDisposable
@inject IComponentBundleManager BundleManager
@inject PersistentComponentState PersistentComponentState
@if (ScriptFiles != null)
{
@ -18,16 +20,37 @@
private List<string>? ScriptFiles { get; set; }
private PersistingComponentStateSubscription persistingSubscription;
protected override async Task OnInitializedAsync()
{
if (BundleName == null)
{
throw new AbpException("The BundleName parameter of the AbpScripts component can not be null!");
}
ScriptFiles = (await BundleManager.GetScriptBundleFilesAsync(BundleName!)).ToList();
persistingSubscription = PersistentComponentState.RegisterOnPersisting(PersistScriptFiles);
if (PersistentComponentState.TryTakeFromJson<List<string>>(nameof(ScriptFiles), out var restoredStyleFiles))
{
ScriptFiles = restoredStyleFiles;
}
else
{
ScriptFiles = (await BundleManager.GetScriptBundleFilesAsync(BundleName!)).ToList();
}
if (OperatingSystem.IsBrowser() && WebAssemblyScriptFiles != null)
{
ScriptFiles.AddRange(WebAssemblyScriptFiles);
ScriptFiles?.AddRange(WebAssemblyScriptFiles);
}
}
private Task PersistScriptFiles()
{
PersistentComponentState.PersistAsJson(nameof(ScriptFiles), ScriptFiles);
return Task.CompletedTask;
}
public void Dispose() => persistingSubscription.Dispose();
}

Loading…
Cancel
Save