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:
- Open the attached tiddlywiki file (take security precautions before opening the file if necessary)
- 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.
- 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.
- 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.