[Pkg-javascript-commits] [leaflet] 76/301: clean up template interpolation code #1969

Jonas Smedegaard js at moszumanska.debian.org
Mon Jan 27 22:22:42 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository leaflet.

commit d0f768ba36ad19df1a6ad8b33de6d4be8aecbc9b
Author: Vladimir Agafonkin <agafonkin at gmail.com>
Date:   Tue Aug 27 21:45:13 2013 +0300

    clean up template interpolation code #1969
---
 src/core/Util.js | 35 +++++++++++------------------------
 1 file changed, 11 insertions(+), 24 deletions(-)

diff --git a/src/core/Util.js b/src/core/Util.js
index 49117e5..e8fb78e 100644
--- a/src/core/Util.js
+++ b/src/core/Util.js
@@ -104,33 +104,20 @@ L.Util = {
 		}
 		return ((!existingUrl || existingUrl.indexOf('?') === -1) ? '?' : '&') + params.join('&');
 	},
-	
+
 	compileTemplate: function (str, data) {
-		/*jslint evil: true */
-		//from https://gist.github.com/padolsey/6008842
-		return new Function(
-			'o',
-			'return "' + (
-				str.replace(/\"/g, '\\"').replace(/\{ *([\w_]+) *\}/g, function (_, $1) {
-					if (typeof data[$1] === 'function') {
-						return '" + o["' + $1 + '"](o) + "';
-					} else {
-						return '" + o["' + $1 + '"] + "';
-					}
-				})
-			) + '";'
-		);
+		// based on https://gist.github.com/padolsey/6008842
+		str = str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
+			return '" + o["' + key + '"]' + (typeof data[key] === 'function' ? '(o)' : '') + ' + "';
+		});
+		// jshint evil: true
+		return new Function('o', 'return "' + str + '";');
 	},
-	
-	templateCache: {},
-	
+
 	template: function (str, data) {
-		if (str in this.templateCache) {
-			return this.templateCache[str](data);
-		} else {
-			this.templateCache[str] = this.compileTemplate(str, data);
-			return this.templateCache[str](data);
-		}
+		var cache = L.Util._templateCache = L.Util._templateCache || {};
+		cache[str] = cache[str] || L.Util.compileTemplate(str, data);
+		return cache[str](data);
 	},
 
 	isArray: function (obj) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet.git



More information about the Pkg-javascript-commits mailing list