Hi Tobias, thanks for the reply.
среда, 30 октября 2013 г., 23:09:13 UTC+4 пользователь Tobias Beer написал:
>
> I think it's generally best to use two functions and would perhaps ask you
> whether or not you really need a dedicated getter.
>
Actually, as the code grew, I ended up with separate setter and getter.
Dedicated getter is needed as for included tiddlers additional stuff
instead of ordinary fields is used.
> Otherwise you could call your function *accessFoo* and have it either get
> or set the value accordingly.
>
Aha, "access" is good enough, thanks.
> I guess creating that object instance is another matter as well. Are we
> even talking about instanciated "custom" objects? Because if we're not,
> then what's the use of having getters and setters? If we are, what type of
> objects are you trying to model... and what properties do you want to get /
> set?
>
currently, those are methods of a macro (you can take a look a the code
below), but I think I should turn them into the methods of the
Tiddlerprototype. The purpose is to store "counters" that are used to sort
aggregated lists (for both included and ).
...
getCounter: function(tiddler,fieldName) {
fieldName = fieldName || this.defaultField;
if(!tiddler.getIncludeURL || !tiddler.getIncludeURL())
return parseInt(store.getValue(tiddler,fieldName));
// for included tiddlers use a separate tiddler as a stored index
var indexText = store.getTiddlerText(fieldName);
// find the line in the index which describes the tiddler, if
present
var indexLineRegExp = this.getIndexLineRegExp(tiddler.title),
indexMatch = indexLineRegExp.exec(indexText);
return indexMatch ? parseInt(indexMatch[1]) : undefined;
},
setCounter: function(tiddler,fieldName,value) {
if(!tiddler.getIncludeURL || !tiddler.getIncludeURL())
return store.setValue(tiddler,fieldName,value);
// for included tiddlers use a separate tiddler as a stored index
if(!store.fetchTiddler(fieldName))
store.createTiddler(fieldName);
var indexText = store.getTiddlerText(fieldName);
// find the line in the index which describes the tiddler, if
present
var indexLineRegExp = this.getIndexLineRegExp(tiddler.title),
indexMatch = indexLineRegExp.exec(indexText);
var newIndexLine = this.createIndexLine(tiddler.title,value);
if(indexMatch)
store.fetchTiddler(fieldName).text =
indexText.replace(indexLineRegExp,newIndexLine);
else
store.fetchTiddler(fieldName).text += (newIndexLine+"\n");
},
Best regards,
Yakov.
>
> Tobias.
>
>
> On Wednesday, 23 October 2013 20:27:47 UTC+2, Yakov wrote:
>>
>> Hello guys,
>>
>> I've got a code styling question, so may be it's ok to post it here,
>> although it can be discussed without the TiddlyWiki context?
>>
>> Here's the case: in a TW plugin, I need a setter and a getter function(s)
>> -- normally they don't do much more than store.getValue and
>> store.setValue, but in some special cases they do additional stuff. I
>> feel it really convenient while coding to have one function which does both
>> things -- it works as getter if the last parameter is undefined, and as
>> setter otherwise. This technique is used in jQuery in some methods like
>> data. However, I'd like to name my function *starting with a verb* and
>> currently I use an ugly name gsetCounter (get/set counter). What would
>> you recommend in this case? Any more suitable name? Any reasons why I have
>> to use two functions (aside this naming issue)?
>>
>> Best regards,
>> Yakov.
>>
>
--
You received this message because you are subscribed to the Google Groups
"TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/tiddlywikidev.
For more options, visit https://groups.google.com/groups/opt_out.