Kaynağa Gözat

Merge pull request #451 from janl/fix/view-prototype-issues

Fix view prototype value lookup issues
tags/v2.1.1
David da Silva Contín 11 yıl önce
ebeveyn
işleme
3533bbd7db
8 değiştirilmiş dosya ile 71 ekleme ve 8 silme
  1. +13
    -5
      mustache.js
  2. +14
    -1
      test/_files/null_lookup_object.js
  3. +6
    -0
      test/_files/null_lookup_object.mustache
  4. +4
    -0
      test/_files/null_lookup_object.txt
  5. +30
    -0
      test/_files/uses_props_from_view_prototype.js
  6. +1
    -0
      test/_files/uses_props_from_view_prototype.mustache
  7. +1
    -0
      test/_files/uses_props_from_view_prototype.txt
  8. +2
    -2
      test/render-helper.js

+ 13
- 5
mustache.js Dosyayı Görüntüle

@@ -29,6 +29,14 @@
return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
}

/**
* Null safe way of checking whether or not an object,
* including its prototype, has a given property
*/
function hasProperty (obj, propName) {
return obj != null && typeof obj === 'object' && (propName in obj);
}

// Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
// See https://github.com/janl/mustache.js/issues/189
var regExpTest = RegExp.prototype.test;
@@ -379,14 +387,14 @@
* `undefined` and we want to avoid looking up parent contexts.
**/
while (value != null && index < names.length) {
if (index === names.length - 1 && value != null)
lookupHit = (typeof value === 'object') &&
value.hasOwnProperty(names[index]);
if (index === names.length - 1)
lookupHit = hasProperty(value, names[index]);
value = value[names[index++]];
}
} else if (context.view != null && typeof context.view === 'object') {
} else {
value = context.view[name];
lookupHit = context.view.hasOwnProperty(name);
lookupHit = hasProperty(context.view, name);
}

if (lookupHit)


+ 14
- 1
test/_files/null_lookup_object.js Dosyayı Görüntüle

@@ -14,5 +14,18 @@
"name": "Chris",
"twitter": undefined
}
]
],
"favorites": {
"color": "blue",
"president": "Bush",
"show": "Futurama"
},
"mascot": {
"name": "Squid",
"favorites": {
"color": "orange",
"president": undefined,
"show": null
}
}
})

+ 6
- 0
test/_files/null_lookup_object.mustache Dosyayı Görüntüle

@@ -1,3 +1,9 @@
{{#fobject}}
{{name}}'s twitter: {{#twitter}}{{.}}{{/twitter}}{{^twitter}}unknown{{/twitter}}.
{{/fobject}}

{{#mascot}}
{{name}}'s favorite color: {{#favorites.color}}{{.}}{{/favorites.color}}{{^favorites.color}}no one{{/favorites.color}}.
{{name}}'s favorite president: {{#favorites.president}}{{.}}{{/favorites.president}}{{^favorites.president}}no one{{/favorites.president}}.
{{name}}'s favorite show: {{#favorites.show}}{{.}}{{/favorites.show}}{{^favorites.show}}none{{/favorites.show}}.
{{/mascot}}

+ 4
- 0
test/_files/null_lookup_object.txt Dosyayı Görüntüle

@@ -1,3 +1,7 @@
Flor's twitter: @florrts.
Miquel's twitter: unknown.
Chris's twitter: unknown.

Squid's favorite color: orange.
Squid's favorite president: no one.
Squid's favorite show: none.

+ 30
- 0
test/_files/uses_props_from_view_prototype.js Dosyayı Görüntüle

@@ -0,0 +1,30 @@
var Aaa = (function () {
function Aaa(x, y) {
this.x = x;
this._y = y;
}
Object.defineProperty(Aaa.prototype, "y", {
get: function () {
return this._y;
},
set: function (value) {
this._y = value;
},
enumerable: true,
configurable: true
});
return Aaa;
})();
var Bbb = (function () {
function Bbb() {
}
return Bbb;
})();

var b = new Bbb();
b.item = new Aaa("0", "00");
b.items = [];
b.items.push({ a: new Aaa("1", "2") });
b.items.push({ a: new Aaa("3", "4") });

(b)

+ 1
- 0
test/_files/uses_props_from_view_prototype.mustache Dosyayı Görüntüle

@@ -0,0 +1 @@
[{{ item.x }};{{ item.y }}]||{{#items}}[{{ a.x }};{{ a.y }} {{#a}}{{y}}{{/a}}]{{/items}}

+ 1
- 0
test/_files/uses_props_from_view_prototype.txt Dosyayı Görüntüle

@@ -0,0 +1 @@
[0;00]||[1;2 2][3;4 4]

+ 2
- 2
test/render-helper.js Dosyayı Görüntüle

@@ -26,12 +26,12 @@ function getPartial(testName) {
}

// You can put the name of a specific test to run in the TEST environment
// variable (e.g. TEST=backslashes vows test/render-test.js)
// variable (e.g. TEST=backslashes mocha test/render-test.js)
var testToRun = process.env.TEST;

var testNames;
if (testToRun) {
testNames = [testToRun];
testNames = testToRun.split(',');
} else {
testNames = fs.readdirSync(_files).filter(function (file) {
return (/\.js$/).test(file);


Yükleniyor…
İptal
Kaydet