[TW5] Setting some fields at startup

閲覧: 177 回
最初の未読メッセージにスキップ

FrD

未読、
2015/10/07 5:46:342015/10/07
To: tiddl...@googlegroups.com
Hi,

I'd like to initialize two fields in a state tiddler at startup with the current year and month :

field "year", value : <<now "YYYY">>
field "month", value : <<now "0MM">>

It's for a calendar macro I'm working on. I'd like the calendar macro to display the current mont of the current year in a tab of the sidebar when opening my TW.

I was tempted to use a default tiddler with some  <$action-setfield .../>
But actionwidgets are only triggered by a button tiddler or some other trigger widget, so it's not possible this way.


I've heard about startup modules. Do I need to write one (for such a simple thing), and what are the guidelines ? Or is there a simpler way ?


Any advice welcome !

Thanks

FrD

Jed Carty

未読、
2015/10/07 6:06:482015/10/07
To: TiddlyWiki
I made a plugin that will run a startup script made of action widgets here.

It should be able to do what you want, or it will at least show you how to make a startup module if you would rather go with that.

FrD

未読、
2015/10/07 6:41:492015/10/07
To: TiddlyWiki
Hi Jed,

Very interesting stuff, but seems to me taking a sledgehammer to crack a nut (for my use-case). With your example and some others at http://tiddlywiki.com/, I think I'll try to write my own simple startup module.
I was wondering what do I put in export.after or export.before so that my module is executed after all other modules ?

FrD

FrD

未読、
2015/10/07 9:53:232015/10/07
To: TiddlyWiki
Hi,

I was wondering if it's possible to have a widget that runs the invokeAction method from inside the render method.
I've beeing trying to do that but without success for now.

Thanks

FrD


FrD

未読、
2015/10/07 12:17:002015/10/07
To: TiddlyWiki
Hi,

Here is my first attempt at a startup module for setting the fields "year" and "month" in "$:/_frd/state/calendar".

If does not work.

Any help welcome.

Thanks.

FrD

/*\
$:/_frd/startup.js
type: application/javascript
module-type: startup

Set up of current year and month

\*/

(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

// Export name and synchronous status
exports
.name = "setcurrentyearandmonth";
exports
.platforms = ["browser"];
exports
.after = ["startup"];
exports
.synchronous = true;

exports
.startup = function() {
   
var date= new Date();
   
var year=date.getFullYear();
   
var month=zeropad(date.getMonth() +1);
    $tw
.wiki.setText("$:/_frd/state/calendar","year","",year);
    $tw
.wiki.setText("$:/_frd/state/calendar","month","",month);
};

function zeropad(num) {
   
var s= "0"+num.toString()+"0";
   
return s.slice(-3,-1);
}

})();



Tobias Beer

未読、
2015/10/07 12:40:362015/10/07
To: tiddl...@googlegroups.com
Hi Frd,

I have no experience with startup modules, but the core has a $tw.utils.pad(value) method.

So this...

exports.startup = function() {
    
var date= new Date();
    
var year=date.getFullYear();
    
var month=zeropad(date.getMonth() +1);
    $tw
.wiki.setText("$:/_frd/state/calendar","year","",year);
    $tw
.wiki.setText("$:/_frd/state/calendar","month","",month);
};

function zeropad(num) {
    
var s= "0"+num.toString()+"0";
    
return s.slice(-3,-1);
}

Could just be...

exports.startup = function() {
   
var date= new Date();

    $tw
.wiki.setText("$:/_frd/state/calendar", "year", undefined, date.getFullYear());
    $tw
.wiki.setText("$:/_frd/state/calendar", "month", undefined, $tw.utils.pad(date.getMonth()+1));
};

I can imagine that using undefined rather than "" as the third parameter to setText() perhaps makes a difference.
A quick test suggests the above works, yay.

Wait, checking again, I see it even worked without my mods.
Did you perhaps not look at the fields but just saw an empty tiddler? ;-)

Best wishes,

— tb

FrD

未読、
2015/10/07 16:41:392015/10/07
To: TiddlyWiki
Hi Tobias,

Thanks for looking.
In fact the two lines :


$tw.wiki.setText("$:/_frd/state/calendar","year","",year);
$tw
.wiki.setText("$:/_frd/state/calendar","month","",month);

work well in a regular widget (I've tested it). So does my zeropad function.
So it's probably a "startup module" thing ...

I'll try to change some parameters or wait for someone who knows better to jump in ...

FrD

FrD

未読、
2015/10/07 17:21:192015/10/07
To: TiddlyWiki
Hi,

Well, my bad : I forgot to click the "add" button when I added the module-type field (set to startup).

So the startup module works now !

FrD

Tobias Beer

未読、
2015/10/08 6:53:012015/10/08
To: tiddl...@googlegroups.com
Hi FrD,
 
forgot to click the "add" button when I added the module-type field (set to startup)

ah, that one ;-)

I hope we will make it to a general solution for this
that also works with CTRL-ENTER for this come next release...

#1939 saving a tiddler with non-empty input fields

Best wishes,

— tb

Jed Carty

未読、
2015/10/08 12:13:382015/10/08
To: TiddlyWiki
As for running the invokeAction method from inside the render method, one thing that Jeremy has been very careful about with tiddlywiki is that rendering shouldn't have any side effects because you have to assume that any part of the wiki may be re-rendered at any time which can cause all sorts of unpredictable things.
So, it is possible to do, but it is generally a bad idea.

You can have a daemon running in the background that listens for events and triggers whatever you want on an event. I made a plugin that lets you trigger different actions when there are updates to tiddlers here, but you could probably make something like that that would update on opening a tiddler or mouse click or any other event that javascript can detect.

FrD

未読、
2015/10/08 16:02:502015/10/08
To: TiddlyWiki
Hi Jed,

I managed to have my startup module working so I won't go further ; it's a bit above my level :-).
Thanks anyway, your work is inspiring !

FrD
全員に返信
投稿者に返信
転送
新着メール 0 件