[TW5] Is there a way to force a refresh/re-render of a tiddler? (Issue explained inside)

160 views
Skip to first unread message

Novan Leon

unread,
Jan 29, 2017, 5:27:07 PM1/29/17
to TiddlyWikiDev
This is a somewhat perplexing issue and I can't seem to find a solution. Maybe someone can help.

Background

I'm trying to add the ability to specify alternate view templates to specific tiddlers and allow users to toggle them on and off using a toolbar button. I did this by making the following changes:

Change #1

Add the code below to line 114 of $:/core/modules/widgets/list.js. This lets users specify a "multiTemplate" attribute which, if set to "true", will prompt the list widget to check the list field of the $:/test/DirectLinks/DirectLinksList tiddler to see if it includes the current tiddler and if it does, apply the $:/test/DirectLinks/ViewTemplate instead of the default $:/core/ui/ViewTemplate.

    //Apply direct links view template
   
if (this.getAttribute("multiTemplate") == "true") {    
       
var directLinksTemplate = "$:/test/DirectLinks/ViewTemplate";
       
var tiddlerList = "$:/test/DirectLinks/DirectLinksList";
       
var applicableTiddlers = $tw.wiki.getTiddlerList(tiddlerList);

       
if (applicableTiddlers.indexOf(title) !== -1 && !isDraft) {
           
template = directLinksTemplate;
       
}
   
}

Change #2

Add the multiTemplate="true" attribute to the list widget in $:/core/ui/PageTemplate/story.

Change #3

Create the custom view template in question, as well as two buttons, one to place in the ViewToolbar to switch the current tiddler to the custom template (i.e. by adding the current tiddler to the direct links list) and one to place in the custom template to switch the current tiddler back to the default view template (i.e. by removing the current tiddler from the direct links list).

Both of the buttons in question contain three action widgets:
  • Listops action to add or remove the current tiddler title to/from the direct links list ($:/test/DirectLinks/DirectLinksList)
  • Close tiddler action to close the current tiddler
  • Navigate action to re-open the current tiddler, presumably with the new template applied


The Issue


Everything seems to work as expected with one exception. When I click the "Show direct links" button to add the current tiddler to the direct links list and reload the tiddler, the tiddler is added to the list but the custom template doesn't apply until I do one of the following things:


  • Refresh the browser
  • Edit the tiddler and cancel/save to return
  • Click the "Show direct links" button on one of the other tiddlers

The last bullet is the one that especially puzzles me. This behavior tells me that there IS a way to refresh/re-render a single tiddler rather than the entire page but I'm at a loss as to how.


Try It For Yourself


To experience this behavior for yourself, do the following:


  1. Open the attached tiddlywiki file (take security precautions before opening the file if necessary)
  2. Click on the "Show direct links" button (image icon on far right) on the "Test Bookmark 8" tiddler. The tiddler should close and re-open near the top of the page. You will notice the name of the tiddler, "Test Bookmark 8" has been added to the list shown in the "Test Tiddler" but it still looks just like any other tiddler.
  3. Click on the "Show direct links" button on the "Test Bookmark 7" tiddler (or any other bookmark tiddler, the ones with a url field defined). The tiddler will close and re-open near the top of the page just like the previous one, but the previous one "Test Bookmark 8" will now appear with the custom template applied.
  4. If you click on the "Show direct links" button for other tiddlers, the same behavior will continue. The one you clicked will move to the top and the previous one will appear with the custom template. The tiddlers with the custom template applied will show correctly from that point forward, all except the one you clicked last. The same thing happens in reverse when removing tiddlers from the list.

Thoughts / Questions


Is there a better way to force the refresh/re-rendering of a specific tiddler? Clearly, closing the tiddler and re-opening it doesn't appear to do this. The nearest I can find is by editing and canceling/saving it to return which always works, or by figuring out the how the behavior above works.

During the course of my testing I tried putting all of the button actions into a custom action widget and executing them together ($:/test/DirectLinks/widgets/action-toggletemplate.js). The results were the same with one exception, when I tried to automate the closing/opening of the second tiddler, I noticed if the tiddlers were navigatedTo from inside the story river, it wouldn't have the same refreshing effect. This may be irrelevant but it's just something I noticed.

I've spend the last two days racking my head over this and trying to find a solution with no luck. I'm hoping maybe someone here with more experience may have encountered this before and be able to offer a solution.
tiddlywiki.html

Novan Leon

unread,
Jan 31, 2017, 2:50:19 PM1/31/17
to tiddly...@googlegroups.com
UPDATE:

Some more things I tried to get the tiddler to render correctly (unsuccessfully):
  • Running a function to delete the tiddler and recreate it with exactly the same values had no perceivable change. There was no visible change whatsoever. I wouldn't know the function had run or not if it weren't for the console log.
  • Running a function to close, delete, recreate, and re-open the tiddler in quick succession had no effect.
  • Running a function to add the tiddler to the list, close and re-open the tiddler, then close and re-open the second tiddler works as explained above but isn't a real solution.

It appears no matter how many close, open, delete, create or other actions I string together into a single function (or by adding additional action widgets to a button widget), the effects of the change don't actually render (i.e. they happen, just aren't visible) until all actions are completed. Can anyone confirm this?


This being the case, is there a way to manually trigger the syncer dispatchTask (for example) so it renders between different actions?


I'm just shooting in the dark at this point.

Mat

unread,
Jan 31, 2017, 5:38:19 PM1/31/17
to TiddlyWikiDev
I must confess that I've not properly read through your descriptions but, off the top of my head, I'm thinking that editing a field in a macro tiddler triggers (I think) a refresh. E.g when you use the EditTextWidget on a macro tiddler.

So... I'm thinking that if you use some toggle to set a value on a macro tiddler, then it should refresh.

And, I'd guess that a "macro tiddler" doesn't really have to contain any macros. It is probably enough that it is tagged $:/tags/Macro.

<:-)

Novan Leon

unread,
Jan 31, 2017, 7:20:53 PM1/31/17
to TiddlyWikiDev
Thank you!

I actually tried this before but I think I forgot to set $:/tags/Macro on the tiddler I was editing. I must have missed this. It's amazing how another set of eyes can catch things you missed. I can't thank you enough. :)

After I tried your suggestion and found that it worked, I did some more experimentation and found an even more elegant solution (IMHO). To trigger a refresh I actually use the action-setfield widget to update the $:/core/ui/PageTemplate/story tiddler. Any field and any value will do I believe. I attached a working copy of the tiddler for those who want to see it with the working solution in place.

I'd still like to know if there's a way to directly call a refresh/re-render of a specific tiddler but this solution is more than adequate for my immediate purposes. Thanks again.


tiddlywiki_v2.html
Reply all
Reply to author
Forward
0 new messages