Skip to content

Commit 8935e35

Browse files
committed
Fire afterInsert when item from array has been inserted
Added different handling of afterInsert event when data is an array. When data is not an array event will be fired as before.
1 parent f2a7887 commit 8935e35

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

jquery-loadTemplate/jquery.loadTemplate-1.5.0.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"use strict";
33
var templates = {},
44
queue = {},
5-
formatters = {};
5+
formatters = {},
6+
isArray;
67

78
function loadTemplate(template, data, options) {
89
var $that = this,
@@ -39,6 +40,7 @@
3940
}, options);
4041

4142
if ($.type(data) === "array") {
43+
isArray = true;
4244
return processArray.call(this, template, data, settings);
4345
}
4446

@@ -97,12 +99,16 @@
9799
settings,
98100
{
99101
async: false,
100-
complete: function () {
102+
complete: function (data) {
101103
if (this.html) {
104+
var insertedElement;
102105
if (doPrepend) {
103-
$that.prepend(this.html());
106+
insertedElement = $(this.html()).prependTo($that);
104107
} else {
105-
$that.append(this.html());
108+
insertedElement = $(this.html()).appendTo($that);
109+
}
110+
if (settings.afterInsert && data) {
111+
settings.afterInsert(insertedElement, data);
106112
}
107113
}
108114
done++;
@@ -228,13 +234,13 @@
228234
} else {
229235
$(this).html($templateHtml);
230236
}
231-
if (settings.afterInsert) {
237+
if (settings.afterInsert && !isArray) {
232238
settings.afterInsert($templateHtml, data);
233239
}
234240
});
235241

236242
if (typeof settings.complete === "function") {
237-
settings.complete.call($(this));
243+
settings.complete.call($(this), data);
238244
}
239245
}
240246

0 commit comments

Comments
 (0)