0

I use an observable inside the updateDocumentTitle function that gets a new value after completing an ajax request. But I noticed that the updateDocumentTitle function doesn't fire again after the observable changes its value.

userShell.prototype.router.updateDocumentTitle = function(instance, instruction) {
  document.title = instance.userArr().name();
};

I tried wrapping the contents of updateDocumentTitle inside a computed observable, but for some reason when I navigate between user pages that are under the same shell (users/100 to users/105), the computed observable gets called as many times as I have navigated between pages without refreshing.

Are there any other successful ways of setting a dynamic document title?

Norbert
  • 2,741
  • 8
  • 56
  • 111

1 Answers1

0

It's a competition between when updateDocumentTitle() fires and when your AJAX request completes successfully.

Where does your AJAX call get made, in which Durandal handler for the viewModel? You have activate, attached, and compositionComplete handlers to choose from.

[EDIT]

You will need to abandon updateDocumentTitle() in this case. Simply create an observable in your viewModel. Update that observable from the activate handler upon successful completion of your AJAX call. Make sure you bind to that observable in your view. We actually do that ourselves for the same reasons you would need to.

For robustness, make sure you provide a default title in the event your AJAX call fails.

  • The ajax call is inside the shell's `activate` function. Wherever I put it, the response will come after the `updateDocumentTitle` has fired. – Norbert Jun 27 '14 at 08:44
  • I have the same ajax that runs on page init and on `:id` change. On init, it finishes before `compositionComplete`, so Durandal overwrites the document title after it finishes the composition. On `:id` change, I used your solution and it worked. Then while I was trying to figure out how to make it work on page init, I noticed that if I leave `updateDocumentTitle()` blank, Durandal doesn't try to change the title after composition. Thanks for the help! – Norbert Jun 28 '14 at 12:14