mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-10-29 14:47:40 +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:
@@ -89,17 +89,19 @@ defaultValue: default value if the variable is not defined
|
||||
*/
|
||||
Widget.prototype.getVariable = function(name,options) {
|
||||
options = options || {};
|
||||
var actualParams = options.params || [];
|
||||
// If the variable doesn't exist then look for a macro module
|
||||
if(!(name in this.variables)) {
|
||||
return this.evaluateMacroModule(name,actualParams,options.defaultValue);
|
||||
}
|
||||
var variable = this.variables[name],
|
||||
value = variable.value || "";
|
||||
var actualParams = options.params || [],
|
||||
parentWidget = this.parentWidget;
|
||||
// Check for the variable defined in the parent widget (or an ancestor in the prototype chain)
|
||||
if(parentWidget && name in parentWidget.variables) {
|
||||
var variable = parentWidget.variables[name],
|
||||
value = variable.value || options.defaultValue;
|
||||
// Substitute any parameters specified in the definition
|
||||
value = this.substituteVariableParameters(value,variable.params,actualParams);
|
||||
value = this.substituteVariableReferences(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) {
|
||||
|
||||
Reference in New Issue
Block a user