소스 검색

dot notation support added

- all tests passing
- updated test to include check that it doesn't break IMPLICIT_OPERATOR
- added fix to ensure that you can access global context from within 'dot notatated' blocks
tags/0.4.0
John Butler 14 년 전
부모
커밋
ba16c76887
1개의 변경된 파일17개의 추가작업 그리고 12개의 파일을 삭제
  1. +17
    -12
      mustache.js

+ 17
- 12
mustache.js 파일 보기

@@ -269,14 +269,17 @@ var Mustache = function() {
} }


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

if(name.match(/([a-z_]+)\.?/ig)){
value = this.walk_context(name, context);
// check for dot notation eg. foo.bar
if(name.match(/([a-z_]+)\./ig)){
value = is_kinda_truthy(this.walk_context(name, context));
}
else{
if(is_kinda_truthy(context[name])) {
value = context[name];
} else if(is_kinda_truthy(this.context[name])) {
value = this.context[name];
}
} }


if(typeof value === "function") { if(typeof value === "function") {
@@ -290,13 +293,15 @@ var Mustache = function() {
}, },


walk_context: function(name, context){ walk_context: function(name, context){
var path = name.split('.')
var value_context = context;
var value = context[path.shift()];
var path = name.split('.');
// if the var doesn't exist in current context, check the top level context
var value_context = (context[path[0]] != undefined) ? context : this.context;
var value = value_context[path.shift()];
while(value != undefined && path.length > 0){ while(value != undefined && path.length > 0){
value_context = value
value_context = value;
value = value[path.shift()]; value = value[path.shift()];
} }
// if the value is a function, call it, binding the correct context
if(typeof value === "function") { if(typeof value === "function") {
return value.apply(value_context); return value.apply(value_context);
} }


불러오는 중...
취소
저장