Skip to content

Commit f19786a

Browse files
committed
Added callbacks before and after template insert
Added two options, beforeInsert and afterInsert, that are callback functions that will be called before inserting the element into the DOM, and after inserting the element into the DOM.
1 parent 02d7f1c commit f19786a

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

Examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<link href='http://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>
1111
<script src="http://yandex.st/highlightjs/7.3/highlight.min.js"></script>
1212
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
13-
<script src="../jquery-loadTemplate/jquery.loadTemplate-1.1.0.min.js"></script>
13+
<script src="../jquery-loadTemplate/jquery.loadTemplate-1.2.0.js"></script>
1414
</head>
1515
<body>
1616
<div class="head row">

jquery-loadTemplate/jquery.loadTemplate-1.1.0.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

jquery-loadTemplate/jquery.loadTemplate-1.1.0.js renamed to jquery-loadTemplate/jquery.loadTemplate-1.2.0.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
pageNo: 1,
2828
elemPerPage: 10,
2929
append: false,
30-
prepend: false
30+
prepend: false,
31+
beforeInsert: null,
32+
afterInsert: null
3133
}, options);
3234

3335
if ($.type(data) === "array") {
@@ -154,12 +156,20 @@
154156
bindData(template, data);
155157

156158
$(this).each(function () {
159+
var $templateHtml = $(template.html());
160+
if (settings.beforeInsert) {
161+
settings.beforeInsert($templateHtml);
162+
}
157163
if (settings.append) {
158-
$(this).append(template.html());
159-
} else if(settings.prepend) {
160-
$(this).prepend(template.html());
164+
165+
$(this).append($templateHtml);
166+
} else if (settings.prepend) {
167+
$(this).prepend($templateHtml);
161168
} else {
162-
$(this).html(template.html());
169+
$(this).html($templateHtml);
170+
}
171+
if (settings.afterInsert) {
172+
settings.afterInsert($templateHtml);
163173
}
164174
});
165175

jquery-loadTemplate/jquery.loadTemplate-1.2.0.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loadTemplate.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"templates",
77
"templating"
88
],
9-
"version": "1.1.0",
9+
"version": "1.2.0",
1010
"author": {
1111
"name": "Paul Burgess and other contributors",
1212
"url": "https://github.com/codepb/jquery-template"

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ There are a number of options the plugin accepts. These are:
133133
- "paged" (default false) - A boolean flag to indicate whether arrays should be paged.
134134
- "pageNo" (default 1) - An integer for which page to display if the data is being paged.
135135
- "elemPerPage" (default 10) - The number of elements to display per page if the data is being paged.
136+
- "append" (default false) - If set to true, the template will be appended to the element rather than replacing the contents of the element.
137+
- "prepend" (default false) - If set to true, the template will be prepended to the element rather than replacing the contents of the element. The append option takes priority over prepend, so if both options are set to true, the element is appended and not prepended.
138+
- "beforeInsert" (default null) - Callback function to be called before inserting the template into the document. The format of the function is function($elem) where $elem is the jQuery object of the populated template about to be inserted into the document.
139+
- "afterInsert" (default null) - As above, a callback function to be called after inserting the template into the document. The format is the same as above.
136140

137141
## Future Plans
138142

0 commit comments

Comments
 (0)