Skip to content

Commit 418c80c

Browse files
authored
Merge pull request #3 from Misfits-BE/develop
1.1.0
2 parents d8507b1 + 6f0707b commit 418c80c

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to `misfits/toastr` will be documented in this file.
44

5+
## 1.1.0 - XXXX-XX-XX
6+
7+
- Fix #1 | Fix typo. (`signleton` to `singleton`)
8+
- Bring the javascript defer option to the package
9+
510
## 1.0.0 - 2018-06-24
611

712
- Initial release

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ Include jQuery and [toastr.js](https://github.com/CodeSeven/toastr) in your mast
2525
{!! Toastr::render() !!}
2626
```
2727

28+
If you defer the loading of your scripts include `Toastr::render(true)`
29+
30+
```html
31+
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" defer></script>
32+
<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js" defer></script>
33+
{!! Toastr::render(true) !!}
34+
```
35+
2836
Call one of these methods in your controllers to insert a toast:
2937
- `Toastr::warning($message, $title = null, $options = [])` - add a warning toast
3038
- `Toastr::error($message, $title = null, $options = [])` - add an error toast

src/Toastr.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,21 @@ public function __construct(SessionManager $session, Repository $config)
5454
/**
5555
* Render the notifications script tag
5656
*
57+
* @param bool $defer Indicates that the resources are loading with deferred option or not.
5758
* @return string
5859
*/
59-
public function render(): string
60+
public function render(bool $defer = false): string
6061
{
6162
$notifications = $this->session->get('toastr::notifications');
6263
$output = '<script type="text/javascript">';
6364
$lastconfig = [];
65+
66+
if ($defer) {
67+
$output = 'window.addEventListener("DOMContentLoaded", function() {
68+
(function($) {
69+
$(document).ready(function() {
70+
$(function(){';
71+
}
6472

6573
if (! $notifications) { // Notifications are empty so register a empty array under the variable.
6674
$notifications = [];
@@ -88,6 +96,13 @@ public function render(): string
8896
(isset($notification['title']) ? ", '" . str_replace("'", "\\'", htmlentities($notification['title'])) . "'" : null) . ');';
8997
}
9098

99+
if ($defer) {
100+
$output = '});
101+
});
102+
})(jQuery);
103+
});';
104+
}
105+
91106
$output .= '</script>';
92107

93108
return $output;

src/ToastrServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function register(): void
4646
* @param mixed $app The application services variable.
4747
* @return Toastr
4848
*/
49-
$this->app->signleton('toastr', function ($app): Toastr {
49+
$this->app->singleton('toastr', function ($app): Toastr {
5050
return new Toastr($app['session'], $app['config']);
5151
});
5252
}

0 commit comments

Comments
 (0)