Skip to content
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

Why swal.fire? #1438

Closed
WillGoldstein opened this issue Mar 6, 2019 · 8 comments
Closed

Why swal.fire? #1438

WillGoldstein opened this issue Mar 6, 2019 · 8 comments

Comments

@WillGoldstein
Copy link

Just wondering, why the addition of a breaking change like .fire which seems completely unnecessary at first glance?

@zenflow
Copy link
Member

zenflow commented Mar 7, 2019

We migrated the code-base to be object-oriented (i.e. we changed the swal function into the Swal class). Therefore to instantiate a swal you would need to now use the new keyword, e.g. new Swal({...options}) instead of just Swal({...options}).

The problem with this is that you do nothing with the result of the call (the instance) you will be violating the eslint "no-new" rule, which is a common rule to be enabled, and part of JavaScript Standard Style.

function sayHi () {
  new Swal('Hi') // violates the eslint "no-new" rule
}

The Swal.fire static method behaves the exact same as the constructor (takes the same arguments, and returns an instance) but does not have this problem.

function sayHi () {
  Swal.fire('Hi') // does not violate the eslint "no-new" rule
}

related: When we did most of the OO refactoring during version 7, we used a hack to allow instantiation without the new keyword, to maintain backwards-compatibility (i.e. you could still instantiate a swal with Swal({...options})) but we dropped that for version 8. See issue #1368.

@WillGoldstein
Copy link
Author

Thanks @zenflow , appreciate the thorough explanation.

@zenflow
Copy link
Member

zenflow commented Mar 7, 2019

@WillGoldstein No problem.. this should also serve other people wondering the same thing

@nafg
Copy link

nafg commented Jun 17, 2019

To be clear, will Swal(...) work just usually have a lint error so the fire method was added, or does it not work and .fire is mandatory?

Also, why not include a (lowercase) swal method for backwards-compatibility and mark it deprecated? Or did you?

@limonte
Copy link
Member

limonte commented Jun 17, 2019

To be clear, will Swal(...) work just usually have a lint error so the fire method was added, or does it not work and .fire is mandatory?

.fire() is mandatory.

Also, why not include a (lowercase) swal method for backwards-compatibility and mark it deprecated? Or did you?

It is included

image

Please read the release notes for more info: https://github.com/sweetalert2/sweetalert2/releases/tag/v8.0.0

@nafg
Copy link

nafg commented Jun 17, 2019 via email

@limonte
Copy link
Member

limonte commented Jun 17, 2019

The lowercase is there for backwards-compatibility, yes. But it is recommended to use Swal instead of swal.

@bigabdoul
Copy link

@WillGoldstein You can use the following in your top-level script (just under the sweetalert2 library) to preserve code that rely on the legacy swal(...) syntax:

if (window.Swal && typeof window.Swal.fire === "function") {
    const original = window.swal;
    window.swal = function () {
        window.Swal.fire.call(original, ...arguments);
    };
}

I hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants