Skip to content

Commit f384e6c

Browse files
authored
feat(AjaxService): redesign AjaxService (#4570)
* refactor: 重构 AjaxService 服务 * refactor: 移除 Ajax 组件 * test: 完善单元测试
1 parent 99090e3 commit f384e6c

File tree

5 files changed

+44
-138
lines changed

5 files changed

+44
-138
lines changed

src/BootstrapBlazor/Components/Ajax/Ajax.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/BootstrapBlazor/Components/Ajax/AjaxService.cs

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ private static RenderFragment RenderComponents() => builder =>
104104
builder.OpenComponent<Dialog>(0);
105105
builder.CloseComponent();
106106

107-
builder.OpenComponent<Ajax>(1);
108-
builder.CloseComponent();
109-
110107
builder.OpenComponent<SweetAlert>(2);
111108
builder.CloseComponent();
112109

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using System.Text.Json;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
/// <summary>
11+
/// Ajax 服务类
12+
/// </summary>
13+
public class AjaxService(IJSRuntime jSRuntime)
14+
{
15+
[NotNull]
16+
private JSModule? _module = null;
17+
18+
private Task<JSModule> LoadModule() => jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/ajax.js");
19+
20+
/// <summary>
21+
/// 调用Ajax方法发送请求
22+
/// </summary>
23+
public async Task<JsonDocument?> InvokeAsync(AjaxOption option, CancellationToken token = default)
24+
{
25+
_module ??= await LoadModule();
26+
return await _module.InvokeAsync<JsonDocument?>("execute", token, option);
27+
}
28+
29+
/// <summary>
30+
/// 调用 Goto 方法跳转其他页面
31+
/// </summary>
32+
/// <param name="url"></param>
33+
/// <param name="token"></param>
34+
public async Task Goto(string url, CancellationToken token = default)
35+
{
36+
_module ??= await LoadModule();
37+
await _module.InvokeVoidAsync("goto", token, url);
38+
}
39+
}

test/UnitTest/Components/AjaxTest.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ public async Task Ajax_Test()
2222

2323
var service = Context.Services.GetRequiredService<AjaxService>();
2424
await service.InvokeAsync(option);
25+
}
2526

26-
Context.RenderComponent<Ajax>();
27-
await service.InvokeAsync(option);
27+
[Fact]
28+
public async Task Goto_Test()
29+
{
30+
var service = Context.Services.GetRequiredService<AjaxService>();
2831
await service.Goto("http://www.blazor.zone");
2932
}
3033
}

0 commit comments

Comments
 (0)