This page is part of a static HTML representation of the TiddlyWiki at https://tiddlywiki.com/dev/

Widget attributes tutorial part II

17th February 2019 at 1:21am

This example will build on the previous one. The only modification will be to add a check to the refresh method. The refreshSelf will only be called if a change to the attributes is detected.

The computeAttributes method returns a Javascript object containing properties for each attribute which has changed. So a check like if (changedAttributes.attr1 || changedAttributes.attr2 || changedAttributes.attr3) etc. can be used to detect the change. See the refresh method in $:/core/modules/widgets/view.js for an example showing the check for multiple attributes.

For this example, message is the only attribute implemented.

/*
Refresh if the attribute value changed since render
*/
MyWidget.prototype.refresh = function(changedTiddlers) {
	// Find which attributes have changed
	var changedAttributes = this.computeAttributes();
	if (changedAttributes.message) {
		this.refreshSelf();
		return true;
	} else {
		return false;
	}
};

The full code can be seen at hello-attribute-optimized.js and here is the result (Widget attributes demo II):

[ { "title": "$:/DefaultTiddlers", "text": "[[hello world widget]]" } ] [ { "title": "hello-attribute-optimized.js", "text": "/*\\\n\nHello, World widget\n\n\\*/\n(function() {\n\n/*jslint node: true, browser: true */\n/*global $tw: false */\n\"use strict\";\n\nvar Widget = require(\"$:/core/modules/widgets/widget.js\").widget;\n\nvar MyWidget = function(parseTreeNode, options) {\n\tthis.initialise(parseTreeNode, options);\n};\n\n/*\nInherit from the base widget class\n*/\nMyWidget.prototype = new Widget();\n\n/*\nRender this widget into the DOM\n*/\nMyWidget.prototype.render = function(parent, nextSibling) {\n\tthis.parentDomNode = parent;\n\tthis.computeAttributes();\n\tvar message = this.getAttribute(\"message\", \"World\");\n\tvar textNode = this.document.createTextNode(\"Hello, \" + message + \"!\");\n\tparent.insertBefore(textNode, nextSibling);\n\tthis.domNodes.push(textNode);\n};\n\n/*\nRefresh if the attribute value changed since render\n*/\nMyWidget.prototype.refresh = function(changedTiddlers) {\n\t// Find which attributes have changed\n\tvar changedAttributes = this.computeAttributes();\n\tif (changedAttributes.message) {\n\t\tthis.refreshSelf();\n\t\treturn true;\n\t} else {\n\t\treturn false;\n\t}\n};\n\nexports.hello = MyWidget;\n\n})();\n", "created": "20190205024846183", "modified": "20190205025110882", "module-type": "widget", "tags": "", "type": "application/javascript" } ] [ { "title": "hello world widget", "text": "\n```\n<$hello/>\n```\n\nRenders as:\n\n<$hello/>\n\n---\n\n```\n<$hello message=\"pale blue dot\"/>\n```\n\nRenders as:\n\n<$hello message=\"pale blue dot\"/>\n\n---\n\n<$edit-text focus=yes tiddler=test tag=input/>\n\n```\n<$hello message={{test!!text}}/>\n```\n\nRenders as:\n\n<$hello message={{test!!text}}/>\n\n" } ] [ { "title": "test", "text": "Alice" } ]

The visible behavior here should be identical to the unoptimized behavior of the previous tutorial.