|
1 | 1 | /*
|
2 | 2 | Author: Conan C. Albrecht <[email protected]>
|
3 | 3 | License: MIT
|
4 |
| - Version: 1.1.22 (March 2017) |
| 4 | + Version: 1.1.23 (March 2017) |
5 | 5 |
|
6 | 6 | Reminder on how to publish to GitHub:
|
7 | 7 | Change the version number in all the files.
|
|
44 | 44 | ajax: {
|
45 | 45 | dataType: 'html',
|
46 | 46 | method: 'GET',
|
47 |
| - success: function(data, status, xhr) { |
48 |
| - console.log($('#custom_modal_id')); |
49 |
| - },// |
50 | 47 | // any other options from the regular $.ajax call (see JQuery docs)
|
51 | 48 | },
|
52 |
| - onShow: function(dlg) { |
53 |
| - console.log('The dialog just showed on the screen!'); |
54 |
| - console.log(dlg); |
55 |
| - }, |
| 49 | +
|
| 50 | + }).done(function(data) { |
| 51 | + console.log('Ajax response is here!'); |
| 52 | +
|
| 53 | + }).create(function(event) { |
| 54 | + console.log('Modal is created but not yet visible,') |
| 55 | +
|
| 56 | + }).show(function(event) { |
| 57 | + console.log('Modal is now showing.') |
| 58 | +
|
| 59 | + }).close(function(event) { |
| 60 | + console.log('Modal just closed!') |
56 | 61 | });
|
57 | 62 |
|
58 | 63 | Closing a dialog: (this is standard bootstrap)
|
|
115 | 120 | // the content of the return.
|
116 | 121 | // The arguments are the ones returned from $.ajax: data, status, xhr.
|
117 | 122 | // This can be a single callback or an array of callbacks.
|
| 123 | + // Note you can also use promise-style syntax with the Ajax promise object: |
| 124 | + // $.loadmodal('url').done(function(data, status, xhr) { |
| 125 | + // console.log(this); |
| 126 | + // }); |
118 | 127 |
|
119 | 128 | onCreate: null, // This method is called after the dialog is created by not yet shown. This allows
|
120 | 129 | // you to adjust the dialog before it shows.
|
121 | 130 | // The arguments are the ones returned from $.ajax: data, status, xhr, and "this" is the dialog element.
|
122 | 131 | // This can be a single callback or an array of callbacks.
|
| 132 | + // Note you can also use promise-style syntax with: |
| 133 | + // $.loadmodal('url').create(function(data, status, xhr) { |
| 134 | + // console.log(this); |
| 135 | + // }); |
123 | 136 |
|
124 | 137 | onShow: null, // if set, this function will be called with a reference to the dialog once it has been
|
125 | 138 | // successfully shown.
|
126 | 139 | // The arguments are the ones sent to the shown event: event, and "this" is the dialog element.
|
127 | 140 | // This can be a single callback or an array of callbacks.
|
128 | 141 | // This is a convenience option - you could also use the Boostrap bs.modal.shown event.
|
| 142 | + // Note you can also use promise-style syntax with: |
| 143 | + // $.loadmodal('url').show(function(event) { |
| 144 | + // console.log(this); |
| 145 | + // }); |
129 | 146 |
|
130 | 147 | onClose: null, // if set, this function will be called with a reference to the dialog upon close/hide,
|
131 | 148 | // just before the dialog elements are removed from the DOM.
|
132 |
| - // The callback has no arguments, and it sets "this" to the dialog element. |
| 149 | + // The arguments are the ones sent to the hide event: event, and "this" is the dialog element. |
133 | 150 | // This can be a single callback or an array of callbacks.
|
134 | 151 | // This is a convenience option - you could also use the Boostrap bs.modal.hide event.
|
| 152 | + // Note you can also use promise-style syntax with: |
| 153 | + // $.loadmodal('url').close(function(event) { |
| 154 | + // console.log(this); |
| 155 | + // }); |
135 | 156 |
|
136 | 157 | }, options);
|
137 | 158 |
|
138 |
| - |
139 | 159 | // ensure we have a url
|
140 | 160 | options.ajax.url = options.ajax.url || options.url;
|
141 | 161 | if (!options.ajax.url) {
|
|
231 | 251 | }//if
|
232 | 252 | }); //shown
|
233 | 253 |
|
234 |
| - // add a callback to the onshow methods once the dialog shows (this doesn't run now) |
| 254 | + // add a callback to the onshow methods once the dialog shows |
235 | 255 | div.on('shown.bs.modal', function(event) {
|
236 | 256 | for (var i = 0; i < options.onShow.length; i++) {
|
237 | 257 | options.onShow[i].apply(div.get(0), [ event ]);
|
238 | 258 | } //for
|
239 | 259 | }); //shown
|
240 | 260 |
|
241 |
| - // event to remove the content on close (this doesn't run now) |
| 261 | + // event to remove the content on close |
242 | 262 | div.on('hidden.bs.modal', function(event) {
|
243 | 263 | // trigger the callbacks
|
244 | 264 | for (var i = 0; i < options.onClose.length; i++) {
|
|
256 | 276 | }); //unshift (add success method)
|
257 | 277 |
|
258 | 278 | // load the content from the server
|
259 |
| - $.ajax(options.ajax); |
| 279 | + var promise = $.ajax(options.ajax); |
| 280 | + |
| 281 | + // add some extra methods to the promise object |
| 282 | + $.extend(promise, { |
| 283 | + create: function(func) { |
| 284 | + options.onCreate.push(func); |
| 285 | + return this; |
| 286 | + },//create |
| 287 | + show: function(func) { |
| 288 | + options.onShow.push(func); |
| 289 | + return this; |
| 290 | + },//show |
| 291 | + close: function(func) { |
| 292 | + options.onClose.push(func); |
| 293 | + return this; |
| 294 | + },//close |
| 295 | + });//extend |
| 296 | + |
| 297 | + // return the promise |
| 298 | + return promise; |
260 | 299 |
|
261 | 300 | }; //loadmodal top-level function
|
262 | 301 |
|
|
0 commit comments