data-bind head title

396 views
Skip to first unread message

Weston Weems

unread,
Oct 2, 2011, 2:54:20 AM10/2/11
to KnockoutJS
Is it easily possible to have a property from view model rendered as a
title?

I tried
<head>
...
<title bind-data="viewModel.pageTitle()"></title>
...
</head>

I never see anything in the header change whatsoever... but if I set a
breakpoit on say a button click... I can evaluate pageTitle and see
what is expected.

Is there any trick? I made the title the very last element in the head
( so as to hopefully load AFTER the js initializations etc.

Weston

Gopal Patel

unread,
Oct 2, 2011, 2:57:27 AM10/2/11
to knock...@googlegroups.com
shouldn't it be ?

<title data-bind="text:viewModel.pageTitle()"></title>

Peter S

unread,
Oct 3, 2011, 7:06:03 AM10/3/11
to KnockoutJS
If you don't specify a DOM object in the ko.applyBindings call it
defaults to using document.body, therefore any bindings on title
wouldn't be evaluated in this scenario as it isn't a child of body.

You could try ko.applyBindings(viewModel, window.document) instead?

Weston Weems

unread,
Oct 3, 2011, 11:25:54 AM10/3/11
to KnockoutJS
First of all... let me start off by apologizing...

My first post I wrote after about 3 days with grand total of about 5hr
sleep total. I did have data-bind... but was so out of it, there was
no chance of success =). I managed to get this to work, by assigning
document.title in an observable.

I'll give the bindings a try... I definitely like that solution
better. Is there any negative connotations for applying the bindings
that high up?

Regards
Weston

Peter S

unread,
Oct 4, 2011, 5:26:41 AM10/4/11
to KnockoutJS
Well, when you call applyBindings, it traverses all the children of
whatever element you specify (or body if you don't) looking for data-
bind attributes so using window.document means it has more elements to
parse.
Potentially the initial bindings phase might take a little longer to
execute (probably not noticibly), but after that it shouldn't have any
effect.
Reply all
Reply to author
Forward
0 new messages