Mustachioed js takes an object-object area when there is not a single one in the current

According to RFC Mustache

The {{name}} tag in the base template will try to find the name key in the current context. If there is no name key, nothing will be provided.

So I expected this:

var template = '{{#anArray}}{{aString}}{{/anArray}}'; var json = { "aString":"ABC", "anArray": [1,{"aString":"DEF"}] }; 

Give me one time:

 "DEF" 

However, mustache.js looks for values ​​in the parent scope. What gives me

 "ABCDEF" 

Does context really mean including all areas for parents?

http://jsfiddle.net/ZG4zd/20/

+7
source share
1 answer

The short answer is yes.

Longer answer. Context.prototype.lookup executes a while loop, looking at the token in the current context and its parent contexts, while there is a parent context.

Corresponding bit of code:

 Context.prototype.lookup = function (name) { var value = this._cache[name]; if (!value) { if (name === ".") { value = this.view; } else { var context = this; //Iterate ancestor contexts while (context) { if (name.indexOf(".") > 0) { var names = name.split("."), i = 0; value = context.view; while (value && i < names.length) { value = value[names[i++]]; } } else { value = context.view[name]; } if (value != null) { break; } context = context.parent; } } this._cache[name] = value; } if (typeof value === "function") { value = value.call(this.view); } return value; }; 
+4
source

All Articles