Procházet zdrojové kódy

re-add constraint in prop lookup, but make property lookups for primitives possible through dot notation

pull/618/head
raymond.lam před 9 roky
rodič
revize
d2f3a48028
3 změnil soubory, kde provedl 18 přidání a 5 odebrání
  1. +16
    -3
      mustache.js
  2. +1
    -1
      test/_files/dot_notation.mustache
  3. +1
    -1
      test/_files/dot_notation.txt

+ 16
- 3
mustache.js Zobrazit soubor

@@ -42,9 +42,19 @@
* including its prototype, has a given property
*/
function hasProperty (obj, propName) {
return obj != null && typeof obj === 'object' && (propName in obj);
}

/**
* Safe way of detecting whether or not the given thing is a primitive and
* whether it has the given property
*/
function primitiveHasOwnProperty (primitive, propName) {
return (
(obj != null && typeof obj === 'object' && (propName in obj))
|| (obj !== undefined && obj.hasOwnProperty(propName))
typeof primitive !== 'object'
&& primitive !== undefined
&& primitive.hasOwnProperty
&& primitive.hasOwnProperty(propName)
);
}

@@ -401,7 +411,10 @@
**/
while (intermediateValue != null && index < names.length) {
if (index === names.length - 1)
lookupHit = hasProperty(intermediateValue, names[index]);
lookupHit = (
hasProperty(intermediateValue, names[index])
|| primitiveHasOwnProperty(intermediateValue, names[index])
);

intermediateValue = intermediateValue[names[index++]];
}


+ 1
- 1
test/_files/dot_notation.mustache Zobrazit soubor

@@ -7,5 +7,5 @@
<h2>Test truthy false values:</h2>
<p>Zero: {{truthy.zero}}</p>
<p>False: {{truthy.notTrue}}</p>
<p>length of string should not be rendered: {{price.currency.name.length}}</p>
<p>length of string should be rendered: {{price.currency.name.length}}</p>
<p>length of an array should be rendered: {{authors.length}}</p>

+ 1
- 1
test/_files/dot_notation.txt Zobrazit soubor

@@ -7,5 +7,5 @@
<h2>Test truthy false values:</h2>
<p>Zero: 0</p>
<p>False: false</p>
<p>length of string should not be rendered: </p>
<p>length of string should be rendered: 3</p>
<p>length of an array should be rendered: 2</p>

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