Skip to content

Instantly share code, notes, and snippets.

@robatwilliams
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robatwilliams/9833970 to your computer and use it in GitHub Desktop.
Save robatwilliams/9833970 to your computer and use it in GitHub Desktop.
Binding the document head with KnockoutJS: http://bl.ocks.org/robatwilliams/raw/9833970/
<!doctype html>
<html>
<head>
<title data-bind="text: title">App: Loading ...</title>
<link rel="stylesheet" id="themeLink" data-bind="attr: { href: selectedTheme().url }"
href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/black-tie/jquery-ui.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-debug.js"></script>
<script src="script.js"></script>
</head>
<body>
<div>
Enter document title: <input data-bind="value: title, valueUpdate: 'afterkeydown'" />
</div>
<div>
Select theme: <select data-bind="options: themes, optionsText: 'name', value: selectedTheme"></select>
</div>
<div id="datepicker"></div>
</body>
</html>
var ViewModel, viewModel;
ViewModel = function () {
this.themes = [
{
name: 'black-tie',
url: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/black-tie/jquery-ui.min.css'
}, {
name: 'le-frog',
url: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/le-frog/jquery-ui.min.css'
}
];
this.title = ko.observable('Initial title');
this.selectedTheme = ko.observable(this.themes[0]);
};
viewModel = new ViewModel();
$(document).ready(function () {
ko.applyBindings(viewModel, document.head.querySelector('title'));
ko.applyBindings(viewModel, document.head.querySelector('#themeLink'));
ko.applyBindings(viewModel);
$('#datepicker').datepicker();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment