Skip to content

Commit

Permalink
First steps for Issue #10
Browse files Browse the repository at this point in the history
Using eval when a string matches {{code}}
  • Loading branch information
Gulix committed Jun 16, 2016
1 parent 1237725 commit c4d0331
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/viewModels/cardTemplateVM.js
Expand Up @@ -49,14 +49,7 @@ function cardTemplateVM(jsonTemplate) {
}
// The object is a string : it is processed with card values
if (typeof(jsonObject) == "string") {
if (jsonObject.indexOf('$') >= 0) {
var valueField = jsonObject.replace('$', '');
return cardVM.getValue(valueField);
} else if (jsonObject.indexOf('?') >= 0) {
var valueField = jsonObject.replace('?', '');
return cardVM.getBoolValue(valueField);
}
return jsonObject;
return cardVM.processString(jsonObject);
}
// The object is a JSON object : each key-value is processed recursively
if (typeof(jsonObject) == "object") {
Expand Down
25 changes: 25 additions & 0 deletions src/viewModels/cardVM.js
Expand Up @@ -87,4 +87,29 @@ function cardVM(editableFields, fields) {
}
}
}

/* Processing the content of a field to get a value */
self.processString = function(processedString) {
if (processedString.indexOf('$') >= 0) {
var valueField = processedString.replace('$', '');
return self.getValue(valueField);
} else if (processedString.indexOf('?') >= 0) {
var valueField = processedString.replace('?', '');
return self.getBoolValue(valueField);
}

// A string that is encapsulated by {{myString}} has to be evaluated as code
var regexFindCode = /^\{\{(.*)\}\}$/g;
var matchCode = regexFindCode.exec(processedString);
if (matchCode != null) {
var evaluatedCode = matchCode[0];
var value = null;
// Need to "translate" field values into code eas
// "text":"{{if (2 == 2) { value = '!Dodge'; } else { value = 'Dodge'; }}}"
eval(evaluatedCode);
return value;
}

return processedString;
}
}

0 comments on commit c4d0331

Please sign in to comment.