-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling pending sweet alerts #457
Comments
too bad this is not the default behavior when there is multiple swal notification :/ |
@kentmw What's the possibility of you making a new release with this fix in it, so it's possible to handle multiple Swal-popups after eachother? |
@inctor I'll write up a guide in the next 15 minutes or so and update the service to the one I'm currently using as to remove some of the original bugs. |
SwalServiceHere is the code with only jquery as a project dependencies other than sweetalert: The Code
Bring this code in anyway you want after sweetalert - if you are using browserify, just add
at the bottom of the file. InitializeOnce it is on the page, initialize the service:
Note: it has not been tested with multiple instances on the same page. For most uses a singleton is what you want anyway. Create As UsualTo create a sweet alert, use the service's swal method exactly like you would use the original swal method from sweetalert.
Go ahead, and try it by copying the service code into the console on http://t4t5.github.io/sweetalert/ CloseYou can close the current swal through the UI or programatically using Please note that when you have a callback added to you alert that should be invoked upon close, this callback will be automatically invoked if the user initiates the close. But if you wish to invoke this callback while closing the alert programatically, call:
I believe this can be handled automatically without the need for a separate method (besides .close), but I haven't refactored it yet. Is OpenThe service also has a bit more to the API. You can call Pending Alert MessageA message about the number of pending swals will appear on the current swal as they queue. You can style this by adding css that targets a span with class "other-messages".
You can also turn off the pending messages altogether by initiailizing the service with:
Obviously, the state this service is does not support i18n (multilingual audiances), you'll have to override the CatchesIf you don't create a swal correctly, like missing title for instance, the swalService will still think there is a alert open though the base swal library never created one. Be careful to fully construct you alerts. |
@kentmw Thanks so much. Fixed my issue of needing to present one alert after another. |
Doesn't seem to work if you try and chain two swals, each of which require the user to click "ok" to continue (and not "cancel") - the second swal is shown, but is then immediately closed |
@QuakePhil do you mind posting the sequential swalService.swal invocations? |
Actually, I got it to work by nesting the 2nd swal in the callback of the first, and setting closeOnConfirm to false in the first one... (this solution works without the need for swalservice, using swal2) The code turned out a bit dry, because of the nesting requirement (and so I can't just open a new swal in the callback, the callback itself has to be a direct call to swal, or maybe there is a way to have an intermediate function between the two swals; I just haven't figured it out) |
@kentmw you are awesome man!! Thanks a lot!! Your wrapper saved my ass. A few notes. As @QuakePhil mentioned there is an issue if there are multiple alerts in the queue, each with nested responses for confirm/cancel. In order to deal with this I am closing the parent swal using the ID immediately after the child is created. And in order to pair this Parent (question) - Child(response) scenario, I have made a tiny modification in the wrapper to append all new swals at the beginning of the array/queue and not at the end. This works with single requests as well. So, consider the following scenario in order to show how the alerts are created:
And here is how the alerts will be displayed to the user:
If you have any questions let me know. Once again thanks a lot man, the wrapper did an awesome work. |
@koullislp I'm glad it was useful :) Do you mind posting your modifications? I'll update my original post after taking a look at it. |
Only one word changed in your code:
And the way I am calling it:
|
Any chance you guys can help me conceptualize this using promises? I've been using this with https://github.com/limonte/sweetalert2, but the author feels an alert queue is "spammy" (sweetalert2/sweetalert2#125). However, it's a perfect solution for my use case, and I'm not really familiar enough with promises to figure out how to create and resolve them using his fork of sweetalert and your addon. I suppose I could add a then(function(){}) and put a resolve() and close() in there, but I don't know if that will actually work. Again, I'm not as well versed on promises, yet. Any suggestions? Basically, I need a way to use this with promises instead of callback functions. |
SweetAlert2 supports modals queue: https://sweetalert2.github.io/#chaining-modals |
Thanks a lot dude, you are awesome! |
@kentmw that swalservice code is amazing, thank you so much (I know swal2.0 fixes this but I'm purposely staying with 1.0 because I find it much simpler and nicer to use, and also has a few features that were removed in 2.0) |
Has anyone here tried to implement a SwalService-like queue in sweetalert2? I know they support chaining, but it requires a much more complex syntax, rather than being able to trivially just make sequential alert calls. For my usecase, which is wrapping the library in a different programming language, that wouldn't work, but SwalService worked wonderfully! |
EDIT: Please look at my later comment for most recent SwalService and guidelines
I know this should be a pull request. But I wanted to add the wrapper I wrote here anyway so I can get feedback if this is a useful addition.
This usage here is that anywhere in your application you can call
swalService.swal()
and if there are open sweet alerts, it will queue them and put a notification on the current sweet alert that there is a pending alert. The swal method will return a unique identifier so you can call close on a specific sweet alert even if its is pending.The text was updated successfully, but these errors were encountered: