Skip to content

Commit aa46877

Browse files
committed
Merge pull request uxsolutions#132 from ansman/patch-2
2 parents 2edb8c5 + aa7ee8d commit aa46877

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ Format a component.
185185

186186
Initializes an datepicker.
187187

188+
### remove
189+
190+
Arguments: None
191+
192+
Remove the datepicker. Removes attached events, internal attached objects, and
193+
added HTML elements.
194+
195+
$('#datepicker').datepicker('remove');
196+
188197
### show
189198

190199
Arguments: None

js/bootstrap-datepicker.js

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,15 @@
4848
if(this.component && this.component.length === 0)
4949
this.component = false;
5050

51+
this._attachEvents();
52+
5153
this.forceParse = true;
5254
if ('forceParse' in options) {
5355
this.forceParse = options.forceParse;
5456
} else if ('dateForceParse' in this.element.data()) {
5557
this.forceParse = this.element.data('date-force-parse');
5658
}
5759

58-
if (this.isInput) {
59-
this.element.on({
60-
focus: $.proxy(this.show, this),
61-
keyup: $.proxy(this.update, this),
62-
keydown: $.proxy(this.keydown, this)
63-
});
64-
} else {
65-
if (this.component && this.hasInput){
66-
// For components that are not readonly, allow keyboard nav
67-
this.element.find('input').on({
68-
focus: $.proxy(this.show, this),
69-
keyup: $.proxy(this.update, this),
70-
keydown: $.proxy(this.keydown, this)
71-
});
72-
73-
this.component.on('click', $.proxy(this.show, this));
74-
} else {
75-
this.element.on('click', $.proxy(this.show, this));
76-
}
77-
}
78-
7960
$(document).on('mousedown', function (e) {
8061
// Clicked outside the datepicker, hide it
8162
if ($(e.target).closest('.datepicker').length == 0) {
@@ -131,6 +112,53 @@
131112
Datepicker.prototype = {
132113
constructor: Datepicker,
133114

115+
_events: [],
116+
_attachEvents: function(){
117+
this._detachEvents();
118+
if (this.isInput) {
119+
this._events = [
120+
[this.element, {
121+
focus: $.proxy(this.show, this),
122+
keyup: $.proxy(this.update, this),
123+
keydown: $.proxy(this.keydown, this)
124+
}]
125+
];
126+
}
127+
else if (this.component && this.hasInput){
128+
this._events = [
129+
// For components that are not readonly, allow keyboard nav
130+
[this.element.find('input'), {
131+
focus: $.proxy(this.show, this),
132+
keyup: $.proxy(this.update, this),
133+
keydown: $.proxy(this.keydown, this),
134+
}],
135+
[this.component, {
136+
click: $.proxy(this.show, this)
137+
}]
138+
];
139+
}
140+
else {
141+
this._events = [
142+
[this.element, {
143+
click: $.proxy(this.show, this)
144+
}]
145+
];
146+
}
147+
for (var i=0, el, ev; i<this._events.length; i++){
148+
el = this._events[i][0];
149+
ev = this._events[i][1];
150+
el.on(ev);
151+
}
152+
},
153+
_detachEvents: function(){
154+
for (var i=0, el, ev; i<this._events.length; i++){
155+
el = this._events[i][0];
156+
ev = this._events[i][1];
157+
el.off(ev);
158+
}
159+
this._events = [];
160+
},
161+
134162
show: function(e) {
135163
this.picker.show();
136164
this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
@@ -169,6 +197,12 @@
169197
date: this.date
170198
});
171199
},
200+
201+
remove: function() {
202+
this._detachEvents();
203+
this.picker.remove();
204+
delete this.element.data().datepicker;
205+
},
172206

173207
getDate: function() {
174208
var d = this.getUTCDate();

0 commit comments

Comments
 (0)