Global setup options on the service registration
arivera12 opened this issue · comments
Is your feature request related to a problem? Please describe.
Yes, I need to setup globally HeightAuto = false
since this breaks pages using flex and height 100%.
This is my case as specified in sweet alert 2
Describe the solution you'd like
Global options setup in the services.AddSweetAlert2()
;
Describe alternatives you've considered
What I have done for now is refactor the methods using FireAsync
.
Used the overload method with the sweet alert options and force HeightAuto = false
.
Additional context
N/A
It's seems to be possible using Swal.mixin({ ...options })
https://sweetalert2.github.io/#configuration
I searched the source code and there is no overload here
https://github.com/Basaingeal/Razor.SweetAlert2/blob/develop/ExtensionMethods.cs
This is an interesting suggestion. I'll consider it for the next major version to coincide with sweetalert2@11
.
For now, I'd recommend using the Swal.Mixin({})
, as this is the sort of use case it was created for. Swal.Mixin({})
creates a SweetAlertMixin
object that you can call FireAsync() off of, with whatever options you pass in when the mixin was created.
How does Swal.Mixin({})
affects the current swal?
I used Swal.Mixin({})
yesterday and it works but makes useless the SweetAlertService
current instance since SweetAlertService
doesn't hold the option set up on SweetAlertMixin
I am using hardly the SweetAlertService
and this would make me go a fix it over thousand of files. It doesn't seems a good way for me. 😢
Something you could try perhaps. Create your own scoped service called HeightAutoFalseSweetAlertService
or something similar.
Inject my SweetAlertService
in the constructor. Create the mixin you want. Store it in a local variable of the service class. Then create a FireAsync
method that just passes the config object through to your mixin.
Then, in your components, replace @inject SweetAlertService Swal;
with @inject HeightAutoFalseSweetAlertService Swal;
It could look something like this
public class HeightAutoFalseSweetAlertService {
private SweetAlertMixin _heightAutoFalseMixin;
public HeightAutoFalseSweetAlertService(SweetAlertService originalSwalService)
{
_heightAutoFalseMixin = originalSwalService.Mixin(new SweetAlertOptions {
HeightAuto = false
});
}
public async FireAsync(SweetAlertOptions options) {
await _heightAutoFalseMixin.FireAsync(options);
}
}
I see but still needs to be replaced on all components.
Why can't we have that mixing option in the services.AddSweetAlert2();
?
I haven't read all the source code but does this will take huge refactorization?
This is what we need to expose
Razor.SweetAlert2/Services/SweetAlertMixin.cs
Line 341 in 75037f6
I won't be exposing that method, as I have no intention of extending the API beyond what is provided by the sweetalert2
library.
I'll look into being able to set defaults at Startup, but that will come with v5
I took a deep look into the source code.
I think we just need to overload AddSweetAlert2
with SweetAlertOptions
and SweetAlertService
constructor with SweetAlertOptions
and chain down those settings.
@Basaingeal The MVP!
Can you share the method call that is resulting in this? Your FireAsync call and the SweetAlertOptions that are being passed in?
await SweetAlertService.FireAsync(null, ResourceManagerLocalizer[Response?.Response?.Message ?? "TransactionCompletedSuccessfully"], SweetAlertIcon.Success);
I am not passing any options at all I am using the overload method with only title, message and icon.
Yes. That is documented functionality. If you don't call the method with an options object on the C# side, it won't call the method with an options object on the JavaScript side.
For example Swal.FireAsync("Hello World")
in C# interpolates to Swal.fire("Hello World")
in JavaScript, while Swal.FireAsync(new SweetAlertOptions("Hello World")
in C# will interpolate to Swal.fire({title: "Hello World"})
in JavaScript. If you call a method that doesn't use options, no default options can be applied.
I see. Will try again tomorrow.
It worked! Thanks!