Skip to content
This repository was archived by the owner on Aug 31, 2018. It is now read-only.

Commit a36f4e3

Browse files
author
Matt R. Wilson
committed
Properly attach and merge cached objects for $().tmpl() usage.
A recent update added the the use of the plugin on current jQuery objects, however, the cached objects were not coming along for the ride. Also added license to package.json.
1 parent 60c7bdf commit a36f4e3

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"version": "0.12.0",
44
"description": "A DOM element based templating engine with a logic-less Zen Coding-like markup, object caching, partials and variables",
55
"directories": {},
6-
"dependencies": {},
6+
"dependencies": {
7+
"jquery": ">=1.4.0"
8+
},
79
"devDependencies": {
810
"grunt": "~0.4.2",
911
"grunt-contrib-jasmine": "^0.6.5",
@@ -15,5 +17,12 @@
1517
"repository": {
1618
"type": "git",
1719
"url": "[email protected]:mastermatt/tmpljs.git"
18-
}
20+
},
21+
"bugs": "https://github.com/mastermatt/tmpljs/issues",
22+
"licenses": [
23+
{
24+
"type": "MIT",
25+
"url": "http://www.opensource.org/licenses/mit-license.php"
26+
}
27+
]
1928
}

specs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Basic templating', function() {
2828

2929
var template = [
3030
"label",
31-
" input"
31+
" input$controls"
3232
];
3333

3434
var nodes = $("<div></div><div></div>");
@@ -38,6 +38,7 @@ describe('Basic templating', function() {
3838
var expected = "<div><label><input></label></div><div><label><input></label></div>";
3939
expect(parent.html()).toBe(expected);
4040
expect(returned instanceof $).toBe(true);
41+
expect(returned.cache.controls.length).toBe(2);
4142
});
4243

4344
it('should nest DOM elements', function() {

tmpl.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,23 @@
271271
$.tmpl = tmpl;
272272

273273
$.fn.tmpl = function(template, data, partials) {
274-
return this.empty().each(function() {
275-
$(this).append(tmpl(template, data, partials));
274+
275+
var self = this;
276+
var cache = self.c = self.cache = self.c || {};
277+
var compiled;
278+
var key;
279+
280+
return self.each(function() {
281+
compiled = tmpl(template, data, partials);
282+
$(this).append(compiled);
283+
284+
for (key in compiled.c) {
285+
if (cache[key]) {
286+
cache[key].push(compiled.c[key]);
287+
} else {
288+
cache[key] = compiled.c[key];
289+
}
290+
}
276291
});
277292
};
278293

tmpl.min.js

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

0 commit comments

Comments
 (0)