Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The $let widget #6148

Merged
merged 5 commits into from Oct 30, 2021
Merged

The $let widget #6148

merged 5 commits into from Oct 30, 2021

Conversation

flibbles
Copy link
Contributor

Here is the $let widget, which takes advantage of orderedAttributes in order to allow for interdependent variables, as well as multiple definitions of the same variable.

I made a few design decisions while making this:

  • I broke some code out of computeAttributes in widget.js" into a computeAttribute method, which is a zero-side-effect method which just returns the value of a passed attribute. I did this to cut down on repetition when $let makes its own computeAttributes method
  • I opted not to have the core widget's computeAttributes evaluate attributes in order the way $let does. Since not all widgets have orderedAttributes, and not all attributes in widgets have their "name" member filled out, it made for some ugly logic. But since the order was (and remains) undefined, this can be changed later if desired.

Otherwise, this does everything it's promised to do. It took some special care to make sure it doesn't refresh unnecessarily, but it should all be good now. Also included some documentation (and I touched up varsWidget documentation to use the .wid and .wlink macros when appropriate).

Let me know what ya'll think.

/*
Inherit from the base widget class
*/
LetWidget.prototype = Object.create(Widget.prototype);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern we follow in all the other widgets is to call new Widget() here, which obviates the need to call the constructor later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall I change $vars over too? I copied this code from there.

*/
LetWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if(Object.keys(changedAttributes).length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usual pattern in the core is if($tw.utils.count(changedAttributes) > 0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change this in vars too, which doesn't use this pattern.

};

LetWidget.prototype.getVariableInfo = function(name,options) {
// Special handling: If this variable exists in this very $vars, we can
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be $vars or $let


! Introduction

The <<.wid let>> widget allows multiple variables to be set in one operation. In some situations it can result in simpler code than using the more flexible <<.wlink SetWidget>> widget. It differs from the <<.wlink VarsWidget>> widget in that variables you're defining may depend on earlier variables defined within the same <<.wid let>>.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO should start with: <<.from-version "5.2.1">> The ...

@saqimtiaz
Copy link
Contributor

Exciting stuff, thank you @flibbles

@Jermolene
Copy link
Owner

Thanks @flibbles

I opted not to have the core widget's computeAttributes evaluate attributes in order the way $let does. Since not all widgets have orderedAttributes, and not all attributes in widgets have their "name" member filled out, it made for some ugly logic. But since the order was (and remains) undefined, this can be changed later if desired.

Great, I think that's the right position for the moment.

@flibbles
Copy link
Contributor Author

Applied changes you guys recommended. Let me know if any of you see anything else.

*/
LetWidget.prototype.render = function(parent,nextSibling) {
// Call the constructor
Widget.call(this);
Copy link
Contributor

@saqimtiaz saqimtiaz Oct 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this call is now superfluous, likewise in $vars, otherwise it looks good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Also improved $let test
@saqimtiaz
Copy link
Contributor

Looks good @flibbles. Thank you. This will make me upgrade the wiki I am currently working on to the pre-release as soon as it is merged.

@saqimtiaz
Copy link
Contributor

@Jermolene look forward to your thoughts on this when you have the chance to review.

@Jermolene
Copy link
Owner

Thanks @flibbles, great stuff. Apologies for the delay, I wanted to properly check this out.

@Jermolene Jermolene merged commit 2bfe522 into Jermolene:master Oct 30, 2021
@flibbles flibbles deleted the let branch October 30, 2021 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants