Skip to content

Commit cbd9cb8

Browse files
committed
Update templated list to use IEnumerable<T>
1 parent 8a853d0 commit cbd9cb8

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

docs/08-templated-components.md

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Start by creating a new file `TemplatedList.razor` in the `BlazingComponents` pr
205205
1. Async-loading of any type of data
206206
2. Separate rendering logic for three states - loading, empty list, and showing items
207207

208-
We can solve async loading by accepting a delegate of type `Func<Task<List<?>>>` - we need to figure out what type should replace **?**. Since we want to support any kind of data, we need to declare this component as a generic type. We can make a generic-typed component using the `@typeparam` directive, so place this at the top of `TemplatedList.razor`.
208+
We can solve async loading by accepting a delegate of type `Func<Task<IEnumerable<?>>>` - we need to figure out what type should replace **?**. Since we want to support any kind of data, we need to declare this component as a generic type. We can make a generic-typed component using the `@typeparam` directive, so place this at the top of `TemplatedList.razor`.
209209

210210
```html
211211
@typeparam TItem
@@ -219,9 +219,9 @@ Now that we've defined a generic type parameter we can use it in a parameter dec
219219

220220
```html
221221
@code {
222-
List<TItem> items;
222+
IEnumerable<TItem> items;
223223

224-
[Parameter] public Func<Task<List<TItem>>> Loader { get; set; }
224+
[Parameter] public Func<Task<IEnumerable<TItem>>> Loader { get; set; }
225225

226226
protected override async Task OnParametersSetAsync()
227227
{
@@ -237,7 +237,7 @@ Since we have the data, we can now add the structure of each of the states we ne
237237
{
238238

239239
}
240-
else if (items.Count == 0)
240+
else if (!items.Any())
241241
{
242242
}
243243
else
@@ -270,7 +270,7 @@ Now that we have some `RenderFragment` parameters, we can start using them. Upda
270270
{
271271
@Loading
272272
}
273-
else if (items.Count == 0)
273+
else if (!items.Any())
274274
{
275275
@Empty
276276
}
@@ -295,9 +295,9 @@ Let's add another `string` parameter, and finally the functions block of `Templa
295295

296296
```html
297297
@code {
298-
List<TItem> items;
298+
IEnumerable<TItem> items;
299299

300-
[Parameter] public Func<Task<List<TItem>>> Loader { get; set; }
300+
[Parameter] public Func<Task<IEnumerable<TItem>>> Loader { get; set; }
301301
[Parameter] public RenderFragment Loading { get; set; }
302302
[Parameter] public RenderFragment Empty { get; set; }
303303
[Parameter] public RenderFragment<TItem> Item { get; set; }
@@ -319,7 +319,7 @@ Lastly update the `<div class="list-group">` to contain `<div class="list-group
319319
{
320320
@Loading
321321
}
322-
else if (items.Count == 0)
322+
else if (!items.Any())
323323
{
324324
@Empty
325325
}
@@ -336,9 +336,9 @@ else
336336
}
337337

338338
@code {
339-
List<TItem> items;
339+
IEnumerable<TItem> items;
340340

341-
[Parameter] public Func<Task<List<TItem>>> Loader { get; set; }
341+
[Parameter] public Func<Task<IEnumerable<TItem>>> Loader { get; set; }
342342
[Parameter] public RenderFragment Loading { get; set; }
343343
[Parameter] public RenderFragment Empty { get; set; }
344344
[Parameter] public RenderFragment<TItem> Item { get; set; }
@@ -355,31 +355,27 @@ else
355355

356356
To use the new `TemplatedList` component, we're going to edit `MyOrders.razor`.
357357

358-
First, we need to create a delegate that we can pass to the `TemplatedList` that will load order data. We can do this by keeping the line of code that's in `MyOrders.OnParametersSetAsync` and changing the method signature. The `@code` block should look something like:
358+
First, we need to create a delegate that we can pass to the `TemplatedList` that will load order data. We can do this by keeping the code that's in `MyOrders.OnParametersSetAsync` and changing the method signature. The `@code` block should look something like:
359359

360360
```html
361361
@code {
362-
async Task<List<OrderWithStatus>> LoadOrders()
362+
async Task<IEnumerable<OrderWithStatus>> LoadOrders()
363363
{
364-
var ordersWithStatus = new List<OrderWithStatus>();
365-
var tokenResult = await TokenProvider.RequestAccessToken();
366-
if (tokenResult.TryGetToken(out var accessToken))
364+
var ordersWithStatus = Enumerable.Empty<OrderWithStatus>();
365+
try
367366
{
368-
var request = new HttpRequestMessage(HttpMethod.Get, "orders");
369-
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Value);
370-
var response = await HttpClient.SendAsync(request);
371-
ordersWithStatus = await response.Content.ReadFromJsonAsync<List<OrderWithStatus>>();
367+
ordersWithStatus = await OrdersClient.GetOrders();
372368
}
373-
else
369+
catch (AccessTokenNotAvailableException ex)
374370
{
375-
NavigationManager.NavigateTo(tokenResult.RedirectUrl);
371+
ex.Redirect();
376372
}
377373
return ordersWithStatus;
378374
}
379375
}
380376
```
381377

382-
This matches the signature expected by the `Loader` parameter of `TemplatedList`, it's a `Func<Task<List<?>>>` where the **?** is replaced with `OrderWithStatus` so we are on the right track.
378+
This matches the signature expected by the `Loader` parameter of `TemplatedList`, it's a `Func<Task<IEnumerable<?>>>` where the **?** is replaced with `OrderWithStatus` so we are on the right track.
383379

384380
You can use the `TemplatedList` component now like so:
385381

save-points/08-templated-components/BlazingComponents/TemplatedList.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
@Loading
66
}
7-
else if (items.Count == 0)
7+
else if (!items.Any())
88
{
99
@Empty
1010
}
@@ -21,9 +21,9 @@ else
2121
}
2222

2323
@code {
24-
List<TItem> items;
24+
IEnumerable<TItem> items;
2525

26-
[Parameter] public Func<Task<List<TItem>>> Loader { get; set; }
26+
[Parameter] public Func<Task<IEnumerable<TItem>>> Loader { get; set; }
2727
[Parameter] public RenderFragment Loading { get; set; }
2828
[Parameter] public RenderFragment Empty { get; set; }
2929
[Parameter] public RenderFragment<TItem> Item { get; set; }

save-points/09-progressive-web-app/BlazingComponents/TemplatedList.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
@Loading
66
}
7-
else if (items.Count == 0)
7+
else if (!items.Any())
88
{
99
@Empty
1010
}
@@ -21,9 +21,9 @@ else
2121
}
2222

2323
@code {
24-
List<TItem> items;
24+
IEnumerable<TItem> items;
2525

26-
[Parameter] public Func<Task<List<TItem>>> Loader { get; set; }
26+
[Parameter] public Func<Task<IEnumerable<TItem>>> Loader { get; set; }
2727
[Parameter] public RenderFragment Loading { get; set; }
2828
[Parameter] public RenderFragment Empty { get; set; }
2929
[Parameter] public RenderFragment<TItem> Item { get; set; }

0 commit comments

Comments
 (0)