Skip to content

Instantly share code, notes, and snippets.

@kaheglar
Created March 15, 2017 14:39
Show Gist options
  • Save kaheglar/10afd535e4970ad9ec6accd62b6ceb03 to your computer and use it in GitHub Desktop.
Save kaheglar/10afd535e4970ad9ec6accd62b6ceb03 to your computer and use it in GitHub Desktop.
Knockout server-side rendering.
const domino = require('domino');
const html = `
<!DOCTYPE html>
<html>
<body>
<ul data-bind="foreach: list">
<li data-bind="text: $data"></>
</ul>
</body>
</html>
`;
const window = domino.createWindow(html);
const document = window.document;
global.document = document;
global.window = window;
const ko = require('knockout');
const viewModel = {
list: [
'John',
'Paul',
'George',
'Ringo'
]
};
const rootElement = document.documentElement;
ko.applyBindings(viewModel, rootElement);
console.log(rootElement.outerHTML);
@kaheglar
Copy link
Author

Uses https://github.com/fgnass/domino a Server-side DOM implementation based on Mozilla's dom.js

@kaheglar
Copy link
Author

$ npm install domino knockout
$ node render-ko

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