Added public "needsFallback" method #34

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants

Hi, this is a great project.

I've made a small modification. I needed to know whether the fallback would be used because I have my own code that manages the document title. I extracted that check into a method which I then exposed in the public API. Seems like it might be useful to others. (I'm not particularly attached to the method name, "needsFallback", btw.)

PS, I didn't include the minified file in this request -- the one I generated was quite different from the original.

Thanks,
Alex

@nonsensery nonsensery Add public API to check whether fallback is needed
Extracted a method to check whether Tinycon should fallback to the window title, and exposed that method in the public API.
0949f7e

I'm not sure I understand the use of that. Are you saying that you manage the title only if Tinycon doesn't? If so, wouldn't it be better to just force Tinycon's fallback to enabled whether or not the icon is modified to simplify keeping things consistent?

Hi,

Let me explain what I'm doing with a more concrete example. I have an existing web app that puts an "unread" count in the window title, along with some other dynamic information -- something like this:

function updateDocTitle() {
  var newTitle = "some text that changes from time to time";

  if ( unreadCount ) {
    newTitle += " - " + unreadCount + " Unread";
  }

  document.title = newTitle;
}

What I'd like to do is use Tinycon to add the unread count as a badge on the favicon where possible. When there is a badge, I don't want to have the unread count in the title. The current fallback mechanism in Tinycon is not robust enough for this, though. In my case, I need some way of knowing whether Tinycon was able to badge the icon so I can omit the unread count when I update the title. So, my code becomes something like this:

function updateDocTitle() {
  var newTitle = "some text that changes from time to time";

  if ( unreadCount  &&  Tinycon.needsFallback() ) {
    newTitle += " - " + unreadCount + " Unread";
  }

  document.title = newTitle;
}

function updateDocIcon() {
  Tinycon.setBubble( unreadCount );
}

The only other option I see would be to enhance the Tinycon fallback mechanism, but turning Tinycon into a dynamic window title management library seems like a waste of effort. I like the current implementation, I just think it needs a hook for developers to step in and provide their own fallback mechanism if the built-in one isn't sufficient.

Thoughts? Advice? Have I convinced you?

Thanks,
Alex

There still seems to be a miscommunication. How is what you're doing any different from this?

Tinycon.setOptions({ fallback: true });

function updateDocTitle() {
  document.title =  "some text that changes from time to time";
  Tinycon.setBubble( unreadCount );
}

Using fallback: true causes Tinycon to only show fallback text in the title if it couldn't badge the icon.

Hi Stephan,

I initially tried something like that, but there were two problems:

  1. If I'm reading the code correctly, Tinycon snapshots the document title (as originalTitle) when it's loaded. My title needs to change from time to time (after the page is loaded), so this doesn't work.
  2. I'd like the fallback text to say something like - 3 Unread, instead of just (3).

It seems like Tinycon could support # 1 with something like this:

var originalTitle = null;
var lastTitle = null;

...

var updateTitle = function () {
  var currentTitle = document.title;

  if ( lastTitle !== currentTitle ) {
    originalTitle = currentTitle;
  }

  if (options.fallback) {
    if ((label + '').length > 0) {
      document.title = '(' + label + ') ' + originalTitle;
    } else {
      document.title = originalTitle;
    }

    lastTitle = document.title;
  }
};

That is, check to see if the title has been changed to something other than what Tinycon last set it to and, if it has, consider that to be the new originalTitle. Or, there could just be a setter for the originalTitle, but where's the fun in that?

As for # 2, a solution might be to provide a function or a regexp/replacement to format the fallback.

As a consumer of this library, I'd really it to have a tight focus. That's why I'd vote to leave Tinycon as it is -- working really well in the simple case -- and just add the hook(s) to allow more advanced stuff to be built around it.

I'm not a contributor to Tinycon, but I rather like your proposed solutions to those two problems. (In fact, I suspect I'd have ended up discoverting the first issue and patching it myself at some point)

To me, they're the simplest way to allow Tinycon to cooperate properly with anything that needs to change the title or has special formatting needs. Breaking the encapsulation of whether the number is showing in the icon or the title just seems like a step backward to me.

Haha, well it's nice to have your support, even if you're not a contributor :-)

Owner

tommoor commented Feb 10, 2014

Just cleaning up, the library now supports dynamic document titles - although I suspect you have since solved this issue :)

tommoor closed this Feb 10, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment