Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cto
g1
Commits
ae08c3c2
Commit
ae08c3c2
authored
Mar 18, 2019
by
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
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
docs/template.md
View file @
ae08c3c2
...
@@ -199,7 +199,7 @@ You can also use the `selector: ...` option. For example:
...
@@ -199,7 +199,7 @@ You can also use the `selector: ...` option. For example:
## $.template dispose
## $.template dispose
Deletes the output of a rendered template, use
`$('...').template('dispose')`
.
Deletes the output of a rendered template, use
`$('...').template('dispose')`
.
This removes the DOM elements last
gener
ated by the template.
This removes the DOM elements last
cre
ated by the template.
This is useful for deleting error messages or previous output that's no longer
This is useful for deleting error messages or previous output that's no longer
relevant.
relevant.
...
@@ -224,8 +224,8 @@ template. But this behavior may change.
...
@@ -224,8 +224,8 @@ template. But this behavior may change.
When a template is rendered or disposed,
`template`
is fired on the source. Attributes:
When a template is rendered or disposed,
`template`
is fired on the source. Attributes:
-
`templatedata`
: the passed data argument
-
`templatedata`
: the passed data argument
(e.g.
`{...}`
,
`'dispose'`
)
-
`target`
: the target nodes rendered or disposed, as a jQuery object
-
`target`
: the target nodes rendered or disposed
(if any)
, as a jQuery object
For example:
For example:
...
...
src/template.js
View file @
ae08c3c2
...
@@ -9,12 +9,16 @@ export function template(data, options) {
...
@@ -9,12 +9,16 @@ export function template(data, options) {
findall
(
this
,
selector
).
each
(
function
()
{
findall
(
this
,
selector
).
each
(
function
()
{
var
$this
=
$
(
this
)
var
$this
=
$
(
this
)
// If we want to dispose the last target, just dispose it.
// If we want to dispose the last target, just dispose it.
if
(
data
===
'
dispose
'
)
if
(
data
===
'
dispose
'
)
{
var
$oldtarget
=
$this
.
data
(
_prev_created
)
if
(
$oldtarget
)
$oldtarget
.
remove
()
return
$this
.
trigger
({
return
$this
.
trigger
({
type
:
'
template
'
,
type
:
'
template
'
,
templatedata
:
data
,
templatedata
:
data
,
target
:
$
this
.
data
(
_prev_created
).
remove
()
target
:
$
oldtarget
})
})
}
var
renderer
=
$this
.
data
(
_renderer
)
var
renderer
=
$this
.
data
(
_renderer
)
// If there's no template function cached, cache it
// If there's no template function cached, cache it
if
(
!
renderer
)
{
if
(
!
renderer
)
{
...
...
test/test-template.html
View file @
ae08c3c2
...
@@ -227,19 +227,32 @@
...
@@ -227,19 +227,32 @@
<!-- Disposes output -->
<!-- Disposes output -->
<div
class=
"dispose-root"
>
<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>
</div>
</div>
<script>
<script>
tape
(
'
$().template("dispose") disposes rendered templates
'
,
function
(
t
)
{
tape
(
'
$().template("dispose") disposes rendered templates
'
,
function
(
t
)
{
t
.
plan
(
4
)
t
.
plan
(
7
)
// 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
)
{
$
(
'
.dispose-root
'
).
one
(
'
template
'
,
function
(
e
)
{
t
.
equal
(
$
(
'
.dispose-root p.dashboard
'
).
length
,
1
)
t
.
equal
(
$
(
'
.dispose-root p.dashboard
'
).
length
,
1
)
}).
template
()
}).
template
({
text
:
'
Dashboard
'
})
// Dispose it. It should not exist. It should trigger a template events
$
(
'
.dispose-root script.dashboard
'
).
one
(
'
template
'
,
function
(
e
)
{
$
(
'
.dispose-root script.dashboard
'
).
one
(
'
template
'
,
function
(
e
)
{
t
.
equal
(
$
(
'
.dispose-root p.dashboard
'
).
length
,
0
)
t
.
equal
(
$
(
'
.dispose-root p.dashboard
'
).
length
,
0
)
t
.
equal
(
e
.
templatedata
,
'
dispose
'
)
t
.
equal
(
e
.
templatedata
,
'
dispose
'
)
t
.
ok
(
e
.
target
.
is
(
'
p.dashboard
'
))
t
.
ok
(
e
.
target
.
is
(
'
p.dashboard
'
))
}).
template
(
'
dispose
'
)
}).
template
(
'
dispose
'
)
// If data-target is specified, the contents are removed but not the target
$
(
'
.dispose-root script.dashboard
'
).
one
(
'
template
'
,
function
(
e
)
{
t
.
ok
(
e
.
target
.
parent
().
is
(
'
.dispose-target
'
))
t
.
equal
(
text
(
e
.
target
),
'
Dashboard
'
)
}).
template
({
text
:
'
Dashboard
'
},
{
target
:
'
.dispose-target
'
})
$
(
'
.dispose-root script.dashboard
'
).
one
(
'
template
'
,
function
(
e
)
{
t
.
equal
(
$
(
'
.dispose-target
'
).
html
(),
''
)
}).
template
(
'
dispose
'
)
})
})
</script>
</script>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment