Tinderbox Forum

Set the badge of a note dynamically

@satikusala showed how to set the badge of a note dynamically (I found no reference where… - but I know he did… so this was his idea, not mine).
I modified the code a little bit - still trying to learn TBX. So I created a small function to set the badge:

function checkBadge_db(){
	var foundNoBatch = true;
	$BadgeTags("/gPreferences").keys.each(x){
		if($NLBadge.icontains(x)){
			$Badge = $BadgeTags("/gPreferences")[x];
			foundNoBatch = false;
		};
	};
	if(foundNoBatch){
		$Badge = "stop";
	};
}

The function refers to a note to store the preferences - I call it “gPreferences” all the time and there you find an attribute called “BadgeTags”. It’s a dictionary containing a key and a value (the name of the badge to be used). Also there is a Tagger running updating an attribute NLBadge.
If you add a new line to the tagger note (tag:term 1;term 2;term 3;) you will also add this term to the BadgeTags attribute of the gPreferences note. Very easy to extend your list of badges without having to touch the function.
set_badge.tbx (112.1 KB)

Here is a link to the original thread: Attaching badge automatically when text added a note - #3 by satikusala.

I like your implementation better. My method was a kluge of conditions that does not scale very well (something I put together in 15 minutes).

The nice thing about yours in the function does not need to be touched for the inputs to work.

Now, here is an interesting question. How might we prioritize badges or signal when there is a conflict?

In this demo, if there are two matching terms, e.g. they both have Bug and See, then one (I can’t figure out which on yet, but I think it is in the order of gPreferences) will always in. If there are multiple maybe you could have a special badge for multiples, or trigger a flag. An interesting thought exercise.

The last entry in gPreference will win the race since the loop will iterate through all entries from 1 to n.
It’s easy to add the case if more then two matches were found. Add a variable var hitCount = 0; and use hitCount += 1; in the each loop. Then you can check if(hitCount>1){} to set a different badge.