[extensions-web] js: Support partials automatically in the templates framework
- From: Jasper St. Pierre <jstpierre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [extensions-web] js: Support partials automatically in the templates framework
- Date: Sat, 31 Mar 2012 20:48:14 +0000 (UTC)
commit c77a2539113e0577acab812e5cb5762d53184fdb
Author: Jasper St. Pierre <jstpierre mecheye net>
Date: Fri Mar 30 18:33:50 2012 -0400
js: Support partials automatically in the templates framework
This requires a modification to mustache to support compiled templates
as partials. This is tracked upstream at:
https://github.com/janl/mustache.js/pull/191
sweettooth/static/js/mustache.js | 8 +++++++-
sweettooth/static/js/templates.js | 26 +++++++++++++++++++-------
2 files changed, 26 insertions(+), 8 deletions(-)
---
diff --git a/sweettooth/static/js/mustache.js b/sweettooth/static/js/mustache.js
index 641cebd..9c61671 100644
--- a/sweettooth/static/js/mustache.js
+++ b/sweettooth/static/js/mustache.js
@@ -476,7 +476,7 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
// This anonymous function wraps the generated function so we can do
// argument coercion, setup some variables, and handle any errors
// encountered while executing it.
- return function (view, partials) {
+ var template = function (view, partials) {
partials = partials || {};
var stack = [view]; // context stack
@@ -487,6 +487,8 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
throw debug(e.error, template, e.line, options.file);
}
};
+ template.compiled = true;
+ return template;
}
// Cache of pre-compiled templates.
@@ -511,6 +513,10 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {};
function compile(template, options) {
options = options || {};
+ if (typeof template === "function" && template.compiled) {
+ return template;
+ }
+
// Use a pre-compiled version from the cache if we have one.
if (options.cache !== false) {
if (!_cache[template]) {
diff --git a/sweettooth/static/js/templates.js b/sweettooth/static/js/templates.js
index 2081ace..3e9cbb6 100644
--- a/sweettooth/static/js/templates.js
+++ b/sweettooth/static/js/templates.js
@@ -2,24 +2,36 @@
define(['templates/templatedata', 'mustache'], function(templatedata) {
var module = {};
+ var partials = module._P = {};
module._T = templatedata;
function compile(template) {
// We have our own template caching, don't use Mustache's.
- return Mustache.compile(v, { cache: false });
+ var compiled = Mustache.compile(template, { cache: false });
+ var wrapper = function(view) {
+ return compiled(view, partials);
+ };
+ wrapper.compiled = true;
+ return wrapper;
}
- function _compileTemplateData(data, out) {
+ function _compileTemplateData(data, out, prefix) {
for (var propname in data) {
- var v = data[propname];
- if (typeof(v) === typeof({}))
- out[propname] = _compileTemplateData(v, {});
+ var v = data[propname], pkey;
+ if (prefix)
+ pkey = prefix + "." + propname;
else
- out[propname] = compile(v);
+ pkey = propname;
+
+ if (typeof(v) === typeof({})) {
+ out[propname] = _compileTemplateData(v, {}, pkey);
+ } else {
+ out[propname] = partials[pkey] = compile(v);
+ }
}
return out;
}
- _compileTemplateData(templatedata, module);
+ _compileTemplateData(templatedata, module, "");
return module;
});
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]