1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-29 23:40:45 +00:00

Fixed problem with widgets variable access

Previously, widgets were reading variables from themselves or their
cascaded ancestors. That means that if a widget sets a variable and
then reads the same variable, it will get the same variable back. That
sounds reasonable, until you consider a widget that wants to modify a
variable - eg the tiddler macro. For example:

```
<$tiddler tiddler={{!!report}}>
<$transclude mode="block" />
</$tiddler>
```

Here we first evaluate the `{{!!report}}` reference, which involves
reading the currentTiddler variable, looking up the tiddler, and
retrieving it’s `report` field. The next the tiddler widget is
refreshed, it will use the newly set currentTiddler as the basis for
resolving the `{{!!reference}}` reference.

The fix is to get variables from ancestors, but continue to set them on
ourselves.
This commit is contained in:
Jermolene 2014-05-08 11:51:02 +01:00
parent 6ab68e0fca
commit e60fc9f81f

View File

@ -89,17 +89,19 @@ defaultValue: default value if the variable is not defined
*/ */
Widget.prototype.getVariable = function(name,options) { Widget.prototype.getVariable = function(name,options) {
options = options || {}; options = options || {};
var actualParams = options.params || []; var actualParams = options.params || [],
// If the variable doesn't exist then look for a macro module parentWidget = this.parentWidget;
if(!(name in this.variables)) { // Check for the variable defined in the parent widget (or an ancestor in the prototype chain)
return this.evaluateMacroModule(name,actualParams,options.defaultValue); if(parentWidget && name in parentWidget.variables) {
} var variable = parentWidget.variables[name],
var variable = this.variables[name], value = variable.value || options.defaultValue;
value = variable.value || "";
// Substitute any parameters specified in the definition // Substitute any parameters specified in the definition
value = this.substituteVariableParameters(value,variable.params,actualParams); value = this.substituteVariableParameters(value,variable.params,actualParams);
value = this.substituteVariableReferences(value); value = this.substituteVariableReferences(value);
return value; return value;
}
// If the variable doesn't exist in the parent widget then look for a macro module
return this.evaluateMacroModule(name,actualParams,options.defaultValue);
}; };
Widget.prototype.substituteVariableParameters = function(text,formalParams,actualParams) { Widget.prototype.substituteVariableParameters = function(text,formalParams,actualParams) {