Procházet zdrojové kódy

Remove expensive merge() method. Context lookups for nested sections

and partials are now performed on the current context and the top-
level view object. This avoids a ton of	copying	and duplication	of
view objects and contexts.
tags/0.3.0
Jan Lehnardt před 16 roky
rodič
revize
1e7fce2e7a
1 změnil soubory, kde provedl 24 přidání a 29 odebrání
  1. +24
    -29
      mustache.js

+ 24
- 29
mustache.js Zobrazit soubor

@@ -15,6 +15,7 @@ var Mustache = function() {
pragmas_implemented: { pragmas_implemented: {
"IMPLICIT-ITERATOR": true "IMPLICIT-ITERATOR": true
}, },
context: {},


render: function(template, context, partials, in_recursion) { render: function(template, context, partials, in_recursion) {
// reset buffer // reset buffer
@@ -32,6 +33,7 @@ var Mustache = function() {
} }


if(!in_recursion) { if(!in_recursion) {
this.context = context;
this.buffer = []; this.buffer = [];
} }


@@ -81,7 +83,7 @@ var Mustache = function() {
}, },


/* /*
Tries to find a partial in the global scope and render it
Tries to find a partial in the cuurent scope and render it
*/ */
render_partial: function(name, context, partials) { render_partial: function(name, context, partials) {
name = this.trim(name); name = this.trim(name);
@@ -121,17 +123,17 @@ var Mustache = function() {
} else if(type == "#") { // normal section } else if(type == "#") { // normal section
if(that.is_array(value)) { // Enumerable, Let's loop! if(that.is_array(value)) { // Enumerable, Let's loop!
return that.map(value, function(row) { return that.map(value, function(row) {
return that.render(content, that.merge(context,
that.create_context(row)), partials, true);
return that.render(content, that.create_context(row),
partials, true);
}).join(""); }).join("");
} else if(that.is_object(value)) { // Object, Use it as subcontext! } else if(that.is_object(value)) { // Object, Use it as subcontext!
return that.render(content,
that.merge(context, that.create_context(value)), partials, true);
return that.render(content, that.create_context(value),
partials, true);
} else if(typeof value === "function") { } else if(typeof value === "function") {
// higher order section // higher order section
return value.call(context, content, function(text) { return value.call(context, content, function(text) {
return that.render(text, context, partials, true); return that.render(text, context, partials, true);
})
});
} else if(value) { // boolean section } else if(value) { // boolean section
return that.render(content, context, partials, true); return that.render(content, context, partials, true);
} else { } else {
@@ -208,11 +210,23 @@ var Mustache = function() {
*/ */
find: function(name, context) { find: function(name, context) {
name = this.trim(name); name = this.trim(name);
if(typeof context[name] === "function") {
return context[name].apply(context);

// Checks whether a value is thruthy or false or 0
function is_kinda_truthy(bool) {
return bool === false || bool === 0 || bool;
}

if(is_kinda_truthy(context[name])) {
var value = context[name];
} else if(is_kinda_truthy(this.context[name])) {
var value = this.context[name];
} }
if(context[name] !== undefined) {
return context[name];

if(typeof value === "function") {
return value.apply(context);
}
if(value !== undefined) {
return value;
} }
// silently ignore unkown variables // silently ignore unkown variables
return ""; return "";
@@ -241,25 +255,6 @@ var Mustache = function() {
}); });
}, },


/*
Merges all properties of object `b` into object `a`.
`b.property` overwrites a.property`
*/
merge: function(a, b) {
var _new = {};
for(var name in a) {
if(a.hasOwnProperty(name)) {
_new[name] = a[name];
}
};
for(var name in b) {
if(b.hasOwnProperty(name)) {
_new[name] = b[name];
}
};
return _new;
},

// by @langalex, support for arrays of strings // by @langalex, support for arrays of strings
create_context: function(_context) { create_context: function(_context) {
if(this.is_object(_context)) { if(this.is_object(_context)) {


Načítá se…
Zrušit
Uložit