saving a tiddler based on check box change @ forEachTiddler.

16 views
Skip to first unread message

nad...@gmail.com

unread,
Sep 2, 2008, 6:32:41 PM9/2/08
to TiddlyWiki
saving a tiddler based on check box change @ forEachTiddler.

i spent the last few hours trying to have tiddler saved after changing
a checkbox (using the grate checkbox plugin).
for some reason when using the forEachTiddler plugin i can get the
right tiddler from the object hierarchy.

i read and followed this thread but it is not working to me:

http://groups.google.com/group/TiddlyWiki/browse_thread/thread/8d6de6729de96fcf

here is the "forEachTiddler" code,

<<forEachTiddler
where 'tiddler.tags.contains("Tasks") &&
! tiddler.tags.contains("Completed")'
sortBy 'tiddler.title'

write " ' [X(' +tiddler.title+ ':Completed){}{}{if (!place.checked)
return; var
tid=store.getTiddler(story.findContainingTiddler(place).getAttribute(\"tiddler
\"); displayMessage(tid.title); store.saveTiddler(tid.title,
tid.title, tid.text, tid.modifier, tid.modified, tid.tags,
tid.fields);}] [['+tiddler.title+'|'+tiddler.title+']]\n ' " >>

i can not get the right tid object.

thanks for the help.

Nadav

Eric Shulman

unread,
Sep 2, 2008, 9:23:19 PM9/2/08
to TiddlyWiki
On Sep 2, 3:32 pm, "nad...@gmail.com" <nad...@gmail.com> wrote:
> i spent the last few hours trying to have tiddler saved after changing
> a checkbox (using the great checkbox plugin).
> <<forEachTiddler
> ...
> write " ' [X(' +tiddler.title+ ':Completed){}{}{if (!place.checked)
> return; var
> tid=store.getTiddler(story.findContainingTiddler(place).getAttribute(\"tiddler
> \"); displayMessage(tid.title);  store.saveTiddler(tid.title,
> tid.title, tid.text, tid.modifier, tid.modified, tid.tags,
> tid.fields);}] [['+tiddler.title+'|'+tiddler.title+']]\n ' " >>

There are *three* kinds of quote available for use in a TW macro:
single-quote (') , double-quote ("), and double-squarebrackets
([[...]])

Although <<forEachTiddler>> is very powerful, it's also really easy to
run out of quotes, because the content of the macro's write clause is
enclosed in a set of double-quotes, and also includes single-quotes
surrounding static text that contains double-brackets [[ ]].

Given that you are already writing several bits of javascript code
within the <<forEachTiddler>> macro, perhaps it's not too much of a
stretch to switch over to writing 'pure' inline javascript rather than
do battle with the subtle combination quotes needed by the
<<forEachTiddler>> macro syntax.

For example, here's an inline script that should produce the same
results as the above <<forEachTiddler>> macro:

<script>
var tids = store.getTaggedTiddlers("Tasks");
var fn="if (place.checked) window.saveCurrentTiddler(place); \
var
tid=store.getTiddler(story.findContainingTiddler(here).getAttribute('tiddler');
\
displayMessage(tid.title); \

store.saveTiddler(tid.title,tid.title,tid.text,tid.modifier,tid.modified,tid.tags,tid.fields);";
var out="";
for (var i=0; i<tids.length; i++) {
if (tids[i].isTagged("Completed")) continue;
var t=tids[i].title;
out+=" [X("+t+":Completed){}{}{"+fn+"}] [["+t+"|"+t+"]]\n";
}
return out;
</script>

enjoy,
-e
Eric Shulman
TiddlyTools / ELS Design Studios

Eric Shulman

unread,
Sep 2, 2008, 9:27:11 PM9/2/08
to TiddlyWiki
*sigh* GoogleGroups word-wrapping messed up this line:
------------------------
var fn="if (place.checked) window.saveCurrentTiddler(place); var
tid=store.getTiddler(story.findContainingTiddler(here).getAttribute('tiddler');
displayMessage(tid.title);
store.saveTiddler(tid.title, tid.title, tid.text, tid.modifier,
tid.modified, tid.tags, tid.fields);";
------------------------
It's a quoted text string, so it should all be on one line...

-e

cmari

unread,
Sep 3, 2008, 12:07:48 AM9/3/08
to TiddlyWiki
Eric, is there a way to make this work for tiddler titles that contain
parentheses? Thanks!
cmari

Eric Shulman

unread,
Sep 3, 2008, 1:11:17 AM9/3/08
to TiddlyWiki
> Eric, is there a way to make this work for tiddler titles that contain
> parentheses? Thanks!

Yes... by using the <<checkbox>> *macro* instead of the inline wiki-
syntax... thus, instead of generating a checkbox with this line of
code:

out+=" [X("+t+":Completed){}{}{"+fn+"}] [["+t+"|"+t+"]]\n";

you can write this line of code:

out+=" <<checkbox [["+t+":Completed]] [[]] [[]] [["+fn+"]]>> [["+t
+"|"+t+"]]\n";

That should do it.

nad...@gmail.com

unread,
Sep 3, 2008, 5:44:13 AM9/3/08
to TiddlyWiki
hi eric,

thanks for the help, i used it inline but i still get syntax error
when i checking the checkbox:
"Checkbox onClickAfter error: SyntaxError: missing ) after argument
list"

this is the code i use:



<script>
var tids = store.getTaggedTiddlers("Tasks");
var fn="if (place.checked) window.saveCurrentTiddler(place); var
tid=store.getTiddler(story.findContainingTiddler(here).getAttribute('tiddler');displayMessage(tid.title);
store.saveTiddler(tid.title, tid.title, tid.text, tid.modifier,
tid.modified, tid.tags, tid.fields);";
var out="";
for (var i=0; i<tids.length; i++) {
if (tids[i].isTagged("Completed")) continue;
var t=tids[i].title;
out+=" [X("+t+":Completed){}{}{"+fn+"}] [["+t+"|"+t+"]]\n";
}
return out;
</script>

any ideas ?

Nadav

Eric Shulman

unread,
Sep 3, 2008, 6:15:27 AM9/3/08
to TiddlyWiki
> "Checkbox onClickAfter error: SyntaxError: missing ) after argument list"

I think you might still have a GoogleGroups-induced newline, following
"tid.modifier,"

> store.saveTiddler(tid.title, tid.title, tid.text, tid.modifier,
> tid.modified, tid.tags, tid.fields);";

-e

nad...@gmail.com

unread,
Sep 3, 2008, 7:35:58 AM9/3/08
to TiddlyWiki
i checked and its fine, no newline.
is there an option that im using the wrong InlineJavascriptPlugin
version - i use ver 1.9.3 ?

Eric Shulman

unread,
Sep 3, 2008, 8:52:08 AM9/3/08
to TiddlyWiki
> i checked and its fine, no newline.

Ah! I know where the problem is!!.... Find this line in the script:
var tid=store.getTiddler( ... .getAttribute('tiddler');

and add a ) just before the ; at the end, like this:
var tid=store.getTiddler( ... .getAttribute('tiddler'));

As Bullwinkle Moose says: "This time, fur shur!" :-)

-e

nad...@gmail.com

unread,
Sep 3, 2008, 9:12:00 AM9/3/08
to TiddlyWiki
life is not that simple :-)
the syntax is fine, now i got another error.
I think im missing some function ..

get the error: "Checkbox onClickAfter error: TypeError:
window.saveCurrentTiddler is not a function"

Eric Shulman

unread,
Sep 3, 2008, 10:47:05 AM9/3/08
to TiddlyWiki
> get the error: "Checkbox onClickAfter error: TypeError:
> window.saveCurrentTiddler is not a function"

eek! I really should have tested my code before I posted it!!

"window.saveCurrentTiddler" is a bit of leftover junk from a
preliminary version of the code. That line of code was suppposed to
be replaced... and the reference to 'here' in the next line of code
needs to be 'place' instead.

Thus, the 'onclick' function definition should be written like this
(all one line):

var fn="if (!place.checked) return false; var
tid=store.getTiddler(story.findContainingTiddler(place).getAttribute('tiddler'));
displayMessage(tid.title); store.saveTiddler(tid.title, tid.title,
tid.text, tid.modifier, tid.modified, tid.tags, tid.fields);";


(So much for Bullwinkle Moose! ;-)

-e

nad...@gmail.com

unread,
Sep 3, 2008, 2:08:54 PM9/3/08
to TiddlyWiki
i think Bullwinkle Moose needs update :)
we are done with the errors but the script is not saving the right
tiddler.

this is the code:
<script>
var tids = store.getTaggedTiddlers("Tasks");
var fn="if (!place.checked) return false; var
tid=store.getTiddler(story.findContainingTiddler(place).getAttribute('tiddler'));
store.saveTiddler(tid.title, tid.title,tid.text, tid.modifier,
tid.modified, tid.tags, tid.fields);";
for (var i=0; i<tids.length; i++) {
if (tids[i].isTagged("Completed")) continue;
var t=tids[i].title;
out+=" [X("+t+":Completed){}{}{"+fn+"}] [["+t+"|"+t+"]]\n";
}
return out;
</script>

1. i am getting "undefined" text at the bigginng of the checkbox list
2. when checked, good thing is that i don't get an error, but the
tiddler is saved without the "Completed" tag.

if i understand correctly, when i check the "Completed" checkbox, the
script should first change and add the "Completed" tag to the tiddler
and than save it (with the new Tag). but either it is not saved or
the Tag is not changed.

when i tested it, after checking the checkbox the new tag is not saved
in the DB (we are talking on cctiddly)

should i add "Completed" it to tid.tags ?
any idea what is wrong ?

Eric Shulman

unread,
Sep 3, 2008, 2:41:23 PM9/3/08
to TiddlyWiki
> 1. i am getting "undefined" text at the bigginng of the checkbox list

The script needs:
var out="";
added to it to init the output buffer.

> 2. when checked, good thing is that i don't get an error, but the
> tiddler is saved without the "Completed" tag.

Hmm... that is strange... I will have to look into that... In any
event, you can work around the problem by explicitly setting/clearing
the tag yourself, as part of the 'onAfterClick' processing.

Also, I realized that using 'place' (and place.checked) in the context
of the onAfterClick handler is incorrect... the reference to the
checkbox element itself should be: 'this' instead of 'place'

Here's a revised script incorporating all these tweaks (and this time
I actually tested it... at least briefly!)

<script>
var out="";
var tids = store.getTaggedTiddlers("task");
var fn="var here=story.findContainingTiddler(this); if (!here) return
false; var tid=here.getAttribute('tiddler');
store.setTiddlerTag(tid,this.checked,'Completed'); var
t=store.getTiddler(tid); store.saveTiddler(t.title, t.title,t.text,
t.modifier,t.modified, t.tags, t.fields);";
for (var i=0; i<tids.length; i++) {
if (tids[i].isTagged("Completed")) continue;
var t=tids[i].title;
out+=" [X("+t+":Completed){}{}{"+fn+"}] [["+t+"|"+t+"]]\n";
}
return out;
</script>

Here's hoping this version does the trick! ...

-e

nad...@gmail.com

unread,
Sep 3, 2008, 2:54:03 PM9/3/08
to TiddlyWiki
ok im getting there ...

i moved the call into to the 'for' loop and NOW i am able to TRY and
save the right tiddler.

BUT i get a error when saving (the right tiddler):
"Checkbox onClickAfter error: Error when saving tiddler 'Italy
Traffic': TypeError: tiddler.modified.convertToYYYYMMDDHHMM is not a
function"

here is the modified code:

<script>
out="";
var tids = store.getTaggedTiddlers("Tasks");
for (var i=0; i<tids.length; i++) {
if (tids[i].isTagged("Completed")) continue;
var t=tids[i].title;

out+=" [X("+t+":Completed){}{}{if (!place.checked) return false;
displayMessage('"+t+"'); store.saveTiddler('"+t+"','"+t
+"','"+tids[i].text+"','"+tids[i].modifier+"','"+tids[i].modified
+"','"+tids[i].tags+"','"+tids[i].fields+"');}] [["+t+"|"+t+"]]\n";
}
return out;
</script>


whats that "tiddler.modified.convert" error, do seem to understand.

nad...@gmail.com

unread,
Sep 3, 2008, 2:55:13 PM9/3/08
to TiddlyWiki
btw the last post was before i saw the new version, ill test now

nad...@gmail.com

unread,
Sep 3, 2008, 3:04:28 PM9/3/08
to TiddlyWiki
on tried the last version and for some reason it is not doing
anything, added 'displayMessage' and i didn't get any msg so i think
the script is not executing.

this is the modified ( added 'displayMessage') which is not running.
<script>
var out="";
var tids = store.getTaggedTiddlers("Tasks");
var fn="var here=story.findContainingTiddler(this); if (!here) return
false; var tid=here.getAttribute('tiddler');
store.setTiddlerTag(tid,this.checked,'Completed'); var
t=store.getTiddler(tid); displayMessage(t.title);
store.saveTiddler(t.title, t.title,t.text, t.modifier,t.modified,
t.tags, t.fields);";
for (var i=0; i<tids.length; i++) {
if (tids[i].isTagged("Completed")) continue;
var t=tids[i].title;
out+=" [X("+t+":Completed){}{}{"+fn+"}] [["+t+"|"+t+"]]\n";
}
return out;
</script>

so i think moving it into the 'for' loop is the better way.
do you have an idea why im getting the "tiddler.modified.convert"
error ?

nad...@gmail.com

unread,
Sep 3, 2008, 3:25:39 PM9/3/08
to TiddlyWiki
Hallelujah ...

this is working.

<script>
out="";
var tids = store.getTaggedTiddlers("Tasks");
for (var i=0; i<tids.length; i++) {
if (tids[i].isTagged("Completed")) continue;
var t=tids[i].title;
out+=" [X("+t+":Completed){}{}{if (!place.checked) return false;
displayMessage('"+tids[i].title+"'); var
td=store.getTiddler('"+tids[i].title+"');
store.setTiddlerTag(td,this.checked,'Completed');
store.saveTiddler(td.title, td.title,td.text, td.modifier,td.modified,
td.tags, td.fields);}] [["+t+"|"+t+"]]\n";
}
return out;
</script>



thanks a lot eric for your help.

Nadav
Reply all
Reply to author
Forward
0 new messages