Commit ae08c3c2 authored by S Anand's avatar S Anand
Browse files

BUG: $.template() must allow dispose on uncreated template

parent 0045189e
Pipeline #79797 passed with stage
in 3 minutes and 1 second
......@@ -199,7 +199,7 @@ You can also use the `selector: ...` option. For example:
## $.template dispose
Deletes the output of a rendered template, use `$('...').template('dispose')`.
This removes the DOM elements last generated by the template.
This removes the DOM elements last created by the template.
This is useful for deleting error messages or previous output that's no longer
......@@ -224,8 +224,8 @@ template. But this behavior may change.
When a template is rendered or disposed, `template` is fired on the source. Attributes:
- `templatedata`: the passed data argument
- `target`: the target nodes rendered or disposed, as a jQuery object
- `templatedata`: the passed data argument (e.g. `{...}`, `'dispose'`)
- `target`: the target nodes rendered or disposed (if any), as a jQuery object
For example:
......@@ -9,12 +9,16 @@ export function template(data, options) {
findall(this, selector).each(function () {
var $this = $(this)
// If we want to dispose the last target, just dispose it.
if (data === 'dispose')
if (data === 'dispose') {
var $oldtarget = $
if ($oldtarget)
return $this.trigger({
type: 'template',
templatedata: data,
target: $
target: $oldtarget
var renderer = $
// If there's no template function cached, cache it
if (!renderer) {
......@@ -227,19 +227,32 @@
<!-- Disposes output -->
<div class="dispose-root">
<script type="text/html" class="dashboard"><p class="dashboard">Dashboard</p></script>
<script type="text/html" class="dashboard"><p class="dashboard"><%- text %></p></script>
<div class="dispose-target">Old content deleted</div>
tape('$().template("dispose") disposes rendered templates', function (t) {
// Disposing an un-created template does not raise an error
$('.dispose-root script.dashboard').template('dispose')
// Create the template
$('.dispose-root').one('template', function (e) {
t.equal($('.dispose-root p.dashboard').length, 1)
}).template({text: 'Dashboard'})
// Dispose it. It should not exist. It should trigger a template events
$('.dispose-root script.dashboard').one('template', function (e) {
t.equal($('.dispose-root p.dashboard').length, 0)
t.equal(e.templatedata, 'dispose')
// If data-target is specified, the contents are removed but not the target
$('.dispose-root script.dashboard').one('template', function (e) {
t.equal(text(, 'Dashboard')
}).template({ text: 'Dashboard' }, { target: '.dispose-target' })
$('.dispose-root script.dashboard').one('template', function (e) {
t.equal($('.dispose-target').html(), '')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment