Skip to content

Commit 81c23c7

Browse files
committed
promise pattern with custom methods now supported!
1 parent 85bc47a commit 81c23c7

File tree

3 files changed

+87
-28
lines changed

3 files changed

+87
-28
lines changed

README.txt

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jquery.loadmodal.js
55
Author: Conan C. Albrecht <[email protected]>
66
License: MIT
77

8-
Dependencies:
8+
Dependencies:
99

10-
- JQuery 1.5+
11-
- Bootstrap (tested against v3)
10+
- JQuery 1.7+
11+
- Bootstrap 3
1212

1313

1414
A JQuery plugin to open a Bootstrap modal (dialog) with content loaded via Ajax.
@@ -18,7 +18,7 @@ it easier to call dialogs directly from Javascript without any corresponding
1818
HTML.
1919

2020

21-
Simple example:
21+
Simple example:
2222

2323
$.loadmodal('/your/server/url/');
2424

@@ -27,20 +27,41 @@ Advanced example:
2727

2828
$.loadmodal({
2929
url: '/your/server/url',
30-
id: 'custom_modal_id',
30+
id: 'my-modal-id',
3131
title: 'My Title',
3232
width: '400px',
33+
closeButton: false,
34+
buttons: {
35+
"OK": function() {
36+
// do something here
37+
// a false return here cancels the automatic closing of the dialog
38+
},
39+
"Cancel": false, // no-op - just having the option makes the dialog close
40+
},
41+
modal: {
42+
keyboard: false,
43+
// any other options from the regular $().modal call (see Bootstrap docs)
44+
},
3345
ajax: {
3446
dataType: 'html',
35-
method: 'POST',
36-
success: function(data, status, xhr) {
37-
console.log($('#custom_modal_id'));
38-
},//
47+
method: 'GET',
3948
// any other options from the regular $.ajax call (see JQuery docs)
4049
},
50+
51+
}).done(function(data) {
52+
console.log('Ajax response is here!');
53+
54+
}).create(function(event) {
55+
console.log('Modal is created but not yet visible,')
56+
57+
}).show(function(event) {
58+
console.log('Modal is now showing.')
59+
60+
}).close(function(event) {
61+
console.log('Modal just closed!')
4162
});
4263

4364

4465
Closing a dialog: (this is standard bootstrap)
45-
46-
$('#custom_modal_id').modal('hide');
66+
67+
$('#custom_modal_id').modal('hide');

jquery.loadmodal.js

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Author: Conan C. Albrecht <[email protected]>
33
License: MIT
4-
Version: 1.1.22 (March 2017)
4+
Version: 1.1.23 (March 2017)
55
66
Reminder on how to publish to GitHub:
77
Change the version number in all the files.
@@ -44,15 +44,20 @@
4444
ajax: {
4545
dataType: 'html',
4646
method: 'GET',
47-
success: function(data, status, xhr) {
48-
console.log($('#custom_modal_id'));
49-
},//
5047
// any other options from the regular $.ajax call (see JQuery docs)
5148
},
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!')
5661
});
5762
5863
Closing a dialog: (this is standard bootstrap)
@@ -115,27 +120,42 @@
115120
// the content of the return.
116121
// The arguments are the ones returned from $.ajax: data, status, xhr.
117122
// 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+
// });
118127

119128
onCreate: null, // This method is called after the dialog is created by not yet shown. This allows
120129
// you to adjust the dialog before it shows.
121130
// The arguments are the ones returned from $.ajax: data, status, xhr, and "this" is the dialog element.
122131
// 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+
// });
123136

124137
onShow: null, // if set, this function will be called with a reference to the dialog once it has been
125138
// successfully shown.
126139
// The arguments are the ones sent to the shown event: event, and "this" is the dialog element.
127140
// This can be a single callback or an array of callbacks.
128141
// 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+
// });
129146

130147
onClose: null, // if set, this function will be called with a reference to the dialog upon close/hide,
131148
// 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.
133150
// This can be a single callback or an array of callbacks.
134151
// 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+
// });
135156

136157
}, options);
137158

138-
139159
// ensure we have a url
140160
options.ajax.url = options.ajax.url || options.url;
141161
if (!options.ajax.url) {
@@ -231,14 +251,14 @@
231251
}//if
232252
}); //shown
233253

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
235255
div.on('shown.bs.modal', function(event) {
236256
for (var i = 0; i < options.onShow.length; i++) {
237257
options.onShow[i].apply(div.get(0), [ event ]);
238258
} //for
239259
}); //shown
240260

241-
// event to remove the content on close (this doesn't run now)
261+
// event to remove the content on close
242262
div.on('hidden.bs.modal', function(event) {
243263
// trigger the callbacks
244264
for (var i = 0; i < options.onClose.length; i++) {
@@ -256,7 +276,26 @@
256276
}); //unshift (add success method)
257277

258278
// 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;
260299

261300
}; //loadmodal top-level function
262301

loadmodal.jquery.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
"modal",
88
"dialog"
99
],
10-
"version": "1.1.21",
10+
"version": "1.1.23",
1111
"author": {
1212
"name": "Conan Albrecht",
13-
"url": "http://warp.byu.edu/",
14-
"email": "[email protected]"
13+
"email": "[email protected]"
1514
},
1615
"licenses": [
1716
{
1817
"type": "MIT",
1918
}
2019
],
2120
"dependencies": {
22-
"jquery": ">=1.5",
21+
"jquery": ">=1.7",
2322
"bootstrap": ">=3.0"
2423
},
2524
"homepage": "https://github.com/doconix/jquery.loadmodal.js"

0 commit comments

Comments
 (0)