Skip to content

Commit 26dc17b

Browse files
Johann-SXhmikosR
authored andcommitted
Popover - call content once if it's a function. (twbs#24690)
1 parent b42a38b commit 26dc17b

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

js/src/popover.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ const Popover = (($) => {
124124

125125
// we use append for html objects to maintain js events
126126
this.setElementContent($tip.find(Selector.TITLE), this.getTitle())
127-
this.setElementContent($tip.find(Selector.CONTENT), this._getContent())
127+
let content = this._getContent()
128+
if (typeof content === 'function') {
129+
content = content.call(this.element)
130+
}
131+
this.setElementContent($tip.find(Selector.CONTENT), content)
128132

129133
$tip.removeClass(`${ClassName.FADE} ${ClassName.SHOW}`)
130134
}
@@ -133,9 +137,7 @@ const Popover = (($) => {
133137

134138
_getContent() {
135139
return this.element.getAttribute('data-content')
136-
|| (typeof this.config.content === 'function' ?
137-
this.config.content.call(this.element) :
138-
this.config.content)
140+
|| this.config.content
139141
}
140142

141143
_cleanTipClass() {

js/tests/unit/popover.js

+21
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,25 @@ $(function () {
410410
$popover.trigger($.Event('click'))
411411
}, 200)
412412
})
413+
414+
QUnit.test('popover should call content function only once', function (assert) {
415+
assert.expect(1)
416+
var done = assert.async()
417+
var nbCall = 0
418+
$('<div id="popover" style="display:none">content</div>').appendTo('#qunit-fixture')
419+
var $popover = $('<a href="#">@Johann-S</a>')
420+
.appendTo('#qunit-fixture')
421+
.bootstrapPopover({
422+
content: function () {
423+
nbCall++
424+
return $('#popover').clone().show().get(0)
425+
}
426+
})
427+
.on('shown.bs.popover', function () {
428+
assert.strictEqual(nbCall, 1)
429+
done()
430+
})
431+
432+
$popover.trigger($.Event('click'))
433+
})
413434
})

0 commit comments

Comments
 (0)